1
0

autoindex1.test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. # 2010 April 07
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  6. # May you do good and not evil.
  7. # May you find forgiveness for yourself and forgive others.
  8. # May you share freely, never taking more than you give.
  9. #
  10. #*************************************************************************
  11. # This file implements regression tests for SQLite library. The
  12. # focus of this script is testing automatic index creation logic.
  13. #
  14. set testdir [file dirname $argv0]
  15. source $testdir/tester.tcl
  16. # If the library is not compiled with automatic index support then
  17. # skip all tests in this file.
  18. #
  19. ifcapable {!autoindex} {
  20. finish_test
  21. return
  22. }
  23. # Setup for logging
  24. db close
  25. sqlite3_shutdown
  26. test_sqlite3_log [list lappend ::log]
  27. set ::log [list]
  28. sqlite3 db test.db
  29. # With automatic index turned off, we do a full scan of the T2 table
  30. do_test autoindex1-100 {
  31. db eval {
  32. CREATE TABLE t1(a,b);
  33. INSERT INTO t1 VALUES(1,11);
  34. INSERT INTO t1 VALUES(2,22);
  35. INSERT INTO t1 SELECT a+2, b+22 FROM t1;
  36. INSERT INTO t1 SELECT a+4, b+44 FROM t1;
  37. CREATE TABLE t2(c,d);
  38. INSERT INTO t2 SELECT a, 900+b FROM t1;
  39. }
  40. db eval {
  41. PRAGMA automatic_index=OFF;
  42. SELECT b, d FROM t1 JOIN t2 ON a=c ORDER BY b;
  43. }
  44. } {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988}
  45. do_test autoindex1-101 {
  46. db status step
  47. } {63}
  48. do_test autoindex1-102 {
  49. db status autoindex
  50. } {0}
  51. # With autoindex turned on, we build an index once and then use that index
  52. # to find T2 values.
  53. do_test autoindex1-110 {
  54. db eval {
  55. PRAGMA automatic_index=ON;
  56. SELECT b, d FROM t1 JOIN t2 ON a=c ORDER BY b;
  57. }
  58. } {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988}
  59. do_test autoindex1-111 {
  60. db status step
  61. } {7}
  62. do_test autoindex1-112 {
  63. db status autoindex
  64. } {7}
  65. do_test autoindex1-113 {
  66. set ::log
  67. } {SQLITE_WARNING_AUTOINDEX {automatic index on t2(c)}}
  68. db close
  69. sqlite3_shutdown
  70. test_sqlite3_log
  71. sqlite3_initialize
  72. sqlite3 db test.db
  73. # The same test as above, but this time the T2 query is a subquery rather
  74. # than a join.
  75. do_test autoindex1-200 {
  76. db eval {
  77. PRAGMA automatic_index=OFF;
  78. SELECT b, (SELECT d FROM t2 WHERE c=a) FROM t1;
  79. }
  80. } {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988}
  81. do_test autoindex1-201 {
  82. db status step
  83. } {35}
  84. do_test autoindex1-202 {
  85. db status autoindex
  86. } {0}
  87. do_test autoindex1-210 {
  88. db eval {
  89. PRAGMA automatic_index=ON;
  90. ANALYZE;
  91. UPDATE sqlite_stat1 SET stat='10000' WHERE tbl='t1';
  92. ANALYZE sqlite_master;
  93. SELECT b, (SELECT d FROM t2 WHERE c=a) FROM t1;
  94. }
  95. } {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988}
  96. do_test autoindex1-211 {
  97. db status step
  98. } {7}
  99. do_test autoindex1-212 {
  100. db status autoindex
  101. } {7}
  102. # Modify the second table of the join while the join is in progress
  103. #
  104. do_execsql_test autoindex1-299 {
  105. UPDATE sqlite_stat1 SET stat='10000' WHERE tbl='t2';
  106. ANALYZE sqlite_master;
  107. EXPLAIN QUERY PLAN
  108. SELECT b, d FROM t1 CROSS JOIN t2 ON (c=a);
  109. } {/AUTOMATIC COVERING INDEX/}
  110. do_test autoindex1-300 {
  111. set r {}
  112. db eval {SELECT b, d FROM t1 CROSS JOIN t2 ON (c=a)} {
  113. lappend r $b $d
  114. db eval {UPDATE t2 SET d=d+1}
  115. }
  116. set r
  117. } {11 911 22 922 33 933 44 944 55 955 66 966 77 977 88 988}
  118. do_test autoindex1-310 {
  119. db eval {SELECT d FROM t2 ORDER BY d}
  120. } {919 930 941 952 963 974 985 996}
  121. # The next test does a 10-way join on unindexed tables. Without
  122. # automatic indices, the join will take a long time to complete.
  123. # With automatic indices, it should only take about a second.
  124. #
  125. do_test autoindex1-400 {
  126. db eval {
  127. CREATE TABLE t4(a, b);
  128. INSERT INTO t4 VALUES(1,2);
  129. INSERT INTO t4 VALUES(2,3);
  130. }
  131. for {set n 2} {$n<4096} {set n [expr {$n+$n}]} {
  132. db eval {INSERT INTO t4 SELECT a+$n, b+$n FROM t4}
  133. }
  134. db eval {
  135. SELECT count(*) FROM t4;
  136. }
  137. } {4096}
  138. do_test autoindex1-401 {
  139. db eval {
  140. SELECT count(*)
  141. FROM t4 AS x1
  142. JOIN t4 AS x2 ON x2.a=x1.b
  143. JOIN t4 AS x3 ON x3.a=x2.b
  144. JOIN t4 AS x4 ON x4.a=x3.b
  145. JOIN t4 AS x5 ON x5.a=x4.b
  146. JOIN t4 AS x6 ON x6.a=x5.b
  147. JOIN t4 AS x7 ON x7.a=x6.b
  148. JOIN t4 AS x8 ON x8.a=x7.b
  149. JOIN t4 AS x9 ON x9.a=x8.b
  150. JOIN t4 AS x10 ON x10.a=x9.b;
  151. }
  152. } {4087}
  153. # Ticket [8011086c85c6c404014c947fcf3eb9f42b184a0d] from 2010-07-08
  154. # Make sure automatic indices are not created for the RHS of an IN expression
  155. # that is not a correlated subquery.
  156. #
  157. do_execsql_test autoindex1-500 {
  158. CREATE TABLE t501(a INTEGER PRIMARY KEY, b);
  159. CREATE TABLE t502(x INTEGER PRIMARY KEY, y);
  160. INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES('t501',null,'1000000');
  161. INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES('t502',null,'1000');
  162. ANALYZE sqlite_master;
  163. EXPLAIN QUERY PLAN
  164. SELECT b FROM t501
  165. WHERE t501.a IN (SELECT x FROM t502 WHERE y=?);
  166. } {
  167. 0 0 0 {SEARCH TABLE t501 USING INTEGER PRIMARY KEY (rowid=?)}
  168. 0 0 0 {EXECUTE LIST SUBQUERY 1}
  169. 1 0 0 {SCAN TABLE t502}
  170. }
  171. do_execsql_test autoindex1-501 {
  172. EXPLAIN QUERY PLAN
  173. SELECT b FROM t501
  174. WHERE t501.a IN (SELECT x FROM t502 WHERE y=t501.b);
  175. } {
  176. 0 0 0 {SCAN TABLE t501}
  177. 0 0 0 {EXECUTE CORRELATED LIST SUBQUERY 1}
  178. 1 0 0 {SEARCH TABLE t502 USING AUTOMATIC COVERING INDEX (y=?)}
  179. }
  180. do_execsql_test autoindex1-502 {
  181. EXPLAIN QUERY PLAN
  182. SELECT b FROM t501
  183. WHERE t501.a=123
  184. AND t501.a IN (SELECT x FROM t502 WHERE y=t501.b);
  185. } {
  186. 0 0 0 {SEARCH TABLE t501 USING INTEGER PRIMARY KEY (rowid=?)}
  187. 0 0 0 {EXECUTE CORRELATED LIST SUBQUERY 1}
  188. 1 0 0 {SCAN TABLE t502}
  189. }
  190. # The following code checks a performance regression reported on the
  191. # mailing list on 2010-10-19. The problem is that the nRowEst field
  192. # of ephermeral tables was not being initialized correctly and so no
  193. # automatic index was being created for the emphemeral table when it was
  194. # used as part of a join.
  195. #
  196. do_execsql_test autoindex1-600 {
  197. CREATE TABLE flock_owner(
  198. owner_rec_id INTEGER CONSTRAINT flock_owner_key PRIMARY KEY,
  199. flock_no VARCHAR(6) NOT NULL REFERENCES flock (flock_no),
  200. owner_person_id INTEGER NOT NULL REFERENCES person (person_id),
  201. owner_change_date TEXT, last_changed TEXT NOT NULL,
  202. CONSTRAINT fo_owner_date UNIQUE (flock_no, owner_change_date)
  203. );
  204. CREATE TABLE sheep (
  205. Sheep_No char(7) NOT NULL,
  206. Date_of_Birth char(8),
  207. Sort_DoB text,
  208. Flock_Book_Vol char(2),
  209. Breeder_No char(6),
  210. Breeder_Person integer,
  211. Originating_Flock char(6),
  212. Registering_Flock char(6),
  213. Tag_Prefix char(9),
  214. Tag_No char(15),
  215. Sort_Tag_No integer,
  216. Breeders_Temp_Tag char(15),
  217. Sex char(1),
  218. Sheep_Name char(32),
  219. Sire_No char(7),
  220. Dam_No char(7),
  221. Register_Code char(1),
  222. Colour char(48),
  223. Colour_Code char(2),
  224. Pattern_Code char(8),
  225. Horns char(1),
  226. Litter_Size char(1),
  227. Coeff_of_Inbreeding real,
  228. Date_of_Registration text,
  229. Date_Last_Changed text,
  230. UNIQUE(Sheep_No));
  231. CREATE INDEX fo_flock_no_index
  232. ON flock_owner (flock_no);
  233. CREATE INDEX fo_owner_change_date_index
  234. ON flock_owner (owner_change_date);
  235. CREATE INDEX fo_owner_person_id_index
  236. ON flock_owner (owner_person_id);
  237. CREATE INDEX sheep_org_flock_index
  238. ON sheep (originating_flock);
  239. CREATE INDEX sheep_reg_flock_index
  240. ON sheep (registering_flock);
  241. EXPLAIN QUERY PLAN
  242. SELECT x.sheep_no, x.registering_flock, x.date_of_registration
  243. FROM sheep x LEFT JOIN
  244. (SELECT s.sheep_no, prev.flock_no, prev.owner_person_id,
  245. s.date_of_registration, prev.owner_change_date
  246. FROM sheep s JOIN flock_owner prev ON s.registering_flock =
  247. prev.flock_no
  248. AND (prev.owner_change_date <= s.date_of_registration || ' 00:00:00')
  249. WHERE NOT EXISTS
  250. (SELECT 'x' FROM flock_owner later
  251. WHERE prev.flock_no = later.flock_no
  252. AND later.owner_change_date > prev.owner_change_date
  253. AND later.owner_change_date <= s.date_of_registration||' 00:00:00')
  254. ) y ON x.sheep_no = y.sheep_no
  255. WHERE y.sheep_no IS NULL
  256. ORDER BY x.registering_flock;
  257. } {
  258. 1 0 0 {SCAN TABLE sheep AS s}
  259. 1 1 1 {SEARCH TABLE flock_owner AS prev USING INDEX sqlite_autoindex_flock_owner_1 (flock_no=? AND owner_change_date<?)}
  260. 1 0 0 {EXECUTE CORRELATED SCALAR SUBQUERY 2}
  261. 2 0 0 {SEARCH TABLE flock_owner AS later USING COVERING INDEX sqlite_autoindex_flock_owner_1 (flock_no=? AND owner_change_date>? AND owner_change_date<?)}
  262. 0 0 0 {SCAN TABLE sheep AS x USING INDEX sheep_reg_flock_index}
  263. 0 1 1 {SEARCH SUBQUERY 1 AS y USING AUTOMATIC COVERING INDEX (sheep_no=?)}
  264. }
  265. do_execsql_test autoindex1-700 {
  266. CREATE TABLE t5(a, b, c);
  267. EXPLAIN QUERY PLAN SELECT a FROM t5 WHERE b=10 ORDER BY c;
  268. } {
  269. 0 0 0 {SCAN TABLE t5}
  270. 0 0 0 {USE TEMP B-TREE FOR ORDER BY}
  271. }
  272. # The following checks a performance issue reported on the sqlite-dev
  273. # mailing list on 2013-01-10
  274. #
  275. do_execsql_test autoindex1-800 {
  276. CREATE TABLE accounts(
  277. _id INTEGER PRIMARY KEY AUTOINCREMENT,
  278. account_name TEXT,
  279. account_type TEXT,
  280. data_set TEXT
  281. );
  282. CREATE TABLE data(
  283. _id INTEGER PRIMARY KEY AUTOINCREMENT,
  284. package_id INTEGER REFERENCES package(_id),
  285. mimetype_id INTEGER REFERENCES mimetype(_id) NOT NULL,
  286. raw_contact_id INTEGER REFERENCES raw_contacts(_id) NOT NULL,
  287. is_read_only INTEGER NOT NULL DEFAULT 0,
  288. is_primary INTEGER NOT NULL DEFAULT 0,
  289. is_super_primary INTEGER NOT NULL DEFAULT 0,
  290. data_version INTEGER NOT NULL DEFAULT 0,
  291. data1 TEXT,
  292. data2 TEXT,
  293. data3 TEXT,
  294. data4 TEXT,
  295. data5 TEXT,
  296. data6 TEXT,
  297. data7 TEXT,
  298. data8 TEXT,
  299. data9 TEXT,
  300. data10 TEXT,
  301. data11 TEXT,
  302. data12 TEXT,
  303. data13 TEXT,
  304. data14 TEXT,
  305. data15 TEXT,
  306. data_sync1 TEXT,
  307. data_sync2 TEXT,
  308. data_sync3 TEXT,
  309. data_sync4 TEXT
  310. );
  311. CREATE TABLE mimetypes(
  312. _id INTEGER PRIMARY KEY AUTOINCREMENT,
  313. mimetype TEXT NOT NULL
  314. );
  315. CREATE TABLE raw_contacts(
  316. _id INTEGER PRIMARY KEY AUTOINCREMENT,
  317. account_id INTEGER REFERENCES accounts(_id),
  318. sourceid TEXT,
  319. raw_contact_is_read_only INTEGER NOT NULL DEFAULT 0,
  320. version INTEGER NOT NULL DEFAULT 1,
  321. dirty INTEGER NOT NULL DEFAULT 0,
  322. deleted INTEGER NOT NULL DEFAULT 0,
  323. contact_id INTEGER REFERENCES contacts(_id),
  324. aggregation_mode INTEGER NOT NULL DEFAULT 0,
  325. aggregation_needed INTEGER NOT NULL DEFAULT 1,
  326. custom_ringtone TEXT,
  327. send_to_voicemail INTEGER NOT NULL DEFAULT 0,
  328. times_contacted INTEGER NOT NULL DEFAULT 0,
  329. last_time_contacted INTEGER,
  330. starred INTEGER NOT NULL DEFAULT 0,
  331. display_name TEXT,
  332. display_name_alt TEXT,
  333. display_name_source INTEGER NOT NULL DEFAULT 0,
  334. phonetic_name TEXT,
  335. phonetic_name_style TEXT,
  336. sort_key TEXT,
  337. sort_key_alt TEXT,
  338. name_verified INTEGER NOT NULL DEFAULT 0,
  339. sync1 TEXT,
  340. sync2 TEXT,
  341. sync3 TEXT,
  342. sync4 TEXT,
  343. sync_uid TEXT,
  344. sync_version INTEGER NOT NULL DEFAULT 1,
  345. has_calendar_event INTEGER NOT NULL DEFAULT 0,
  346. modified_time INTEGER,
  347. is_restricted INTEGER DEFAULT 0,
  348. yp_source TEXT,
  349. method_selected INTEGER DEFAULT 0,
  350. custom_vibration_type INTEGER DEFAULT 0,
  351. custom_ringtone_path TEXT,
  352. message_notification TEXT,
  353. message_notification_path TEXT
  354. );
  355. CREATE INDEX data_mimetype_data1_index ON data (mimetype_id,data1);
  356. CREATE INDEX data_raw_contact_id ON data (raw_contact_id);
  357. CREATE UNIQUE INDEX mime_type ON mimetypes (mimetype);
  358. CREATE INDEX raw_contact_sort_key1_index ON raw_contacts (sort_key);
  359. CREATE INDEX raw_contact_sort_key2_index ON raw_contacts (sort_key_alt);
  360. CREATE INDEX raw_contacts_contact_id_index ON raw_contacts (contact_id);
  361. CREATE INDEX raw_contacts_source_id_account_id_index
  362. ON raw_contacts (sourceid, account_id);
  363. ANALYZE sqlite_master;
  364. INSERT INTO sqlite_stat1
  365. VALUES('raw_contacts','raw_contact_sort_key2_index','1600 4');
  366. INSERT INTO sqlite_stat1
  367. VALUES('raw_contacts','raw_contact_sort_key1_index','1600 4');
  368. INSERT INTO sqlite_stat1
  369. VALUES('raw_contacts','raw_contacts_source_id_account_id_index',
  370. '1600 1600 1600');
  371. INSERT INTO sqlite_stat1
  372. VALUES('raw_contacts','raw_contacts_contact_id_index','1600 1');
  373. INSERT INTO sqlite_stat1 VALUES('mimetypes','mime_type','12 1');
  374. INSERT INTO sqlite_stat1
  375. VALUES('data','data_mimetype_data1_index','9819 2455 3');
  376. INSERT INTO sqlite_stat1 VALUES('data','data_raw_contact_id','9819 7');
  377. INSERT INTO sqlite_stat1 VALUES('accounts',NULL,'1');
  378. DROP TABLE IF EXISTS sqlite_stat3;
  379. ANALYZE sqlite_master;
  380. EXPLAIN QUERY PLAN
  381. SELECT * FROM
  382. data JOIN mimetypes ON (data.mimetype_id=mimetypes._id)
  383. JOIN raw_contacts ON (data.raw_contact_id=raw_contacts._id)
  384. JOIN accounts ON (raw_contacts.account_id=accounts._id)
  385. WHERE mimetype_id=10 AND data14 IS NOT NULL;
  386. } {/SEARCH TABLE data .*SEARCH TABLE raw_contacts/}
  387. do_execsql_test autoindex1-801 {
  388. EXPLAIN QUERY PLAN
  389. SELECT * FROM
  390. data JOIN mimetypes ON (data.mimetype_id=mimetypes._id)
  391. JOIN raw_contacts ON (data.raw_contact_id=raw_contacts._id)
  392. JOIN accounts ON (raw_contacts.account_id=accounts._id)
  393. WHERE mimetypes._id=10 AND data14 IS NOT NULL;
  394. } {/SEARCH TABLE data .*SEARCH TABLE raw_contacts/}
  395. finish_test