You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

962 lines
27 KiB

branches/zip: Fast index creation: Remove the ROW_PREBUILT_OBSOLETE nonsense. Active transactions must not switch table or index definitions on the fly, for several reasons, including the following: * copied indexes do not carry any history or locking information; that is, rollbacks, read views, and record locking would be broken * huge potential for race conditions, inconsistent reads and writes, loss of data, and corruption Instead of trying to track down if the table was changed during a transaction, acquire appropriate locks that protect the creation and dropping of indexes. innodb-index.test: Test the locking of CREATE INDEX and DROP INDEX. Test that consistent reads work across dropped indexes. lock_rec_insert_check_and_lock(): Relax the lock_table_has() assertion. When inserting a record into an index, the table must be at least IX-locked. However, when an index is being created, an IS-lock on the table is sufficient. row_merge_lock_table(): Add the parameter enum lock_mode mode, which must be LOCK_X or LOCK_S. row_merge_drop_table(): Assert that n_mysql_handles_opened == 0. Unconditionally drop the table. ha_innobase::add_index(): Acquire an X or S lock on the table, as appropriate. After acquiring an X lock, assert that n_mysql_handles_opened == 1. Remove the comments about dropping tables in the background. ha_innobase::final_drop_index(): Acquire an X lock on the table. dict_table_t: Remove version_number, to_be_dropped, and prebuilts. ins_node_t: Remove table_version_number. enum lock_mode: Move the definition from lock0lock.h to lock0types.h. ROW_PREBUILT_OBSOLETE, row_update_prebuilt(), row_prebuilt_table_obsolete(): Remove. row_prebuilt_t: Remove the declaration from row0types.h. row_drop_table_for_mysql_no_commit(): Always print a warning if a table was added to the background drop queue.
18 years ago
  1. create table t1(a int not null, b int, c char(10) not null, d varchar(20)) engine = innodb;
  2. insert into t1 values (5,5,'oo','oo'),(4,4,'tr','tr'),(3,4,'ad','ad'),(2,3,'ak','ak');
  3. commit;
  4. alter table t1 add index b (b), add index b (b);
  5. ERROR 42000: Duplicate key name 'b'
  6. alter table t1 add index (b,b);
  7. ERROR 42S21: Duplicate column name 'b'
  8. alter table t1 add index d2 (d);
  9. show create table t1;
  10. Table Create Table
  11. t1 CREATE TABLE `t1` (
  12. `a` int(11) NOT NULL,
  13. `b` int(11) DEFAULT NULL,
  14. `c` char(10) NOT NULL,
  15. `d` varchar(20) DEFAULT NULL,
  16. KEY `d2` (`d`)
  17. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  18. explain select * from t1 force index(d2) order by d;
  19. id select_type table type possible_keys key key_len ref rows Extra
  20. 1 SIMPLE t1 index NULL d2 23 NULL 4
  21. select * from t1 force index (d2) order by d;
  22. a b c d
  23. 3 4 ad ad
  24. 2 3 ak ak
  25. 5 5 oo oo
  26. 4 4 tr tr
  27. alter table t1 add unique index (b);
  28. ERROR 23000: Duplicate entry '4' for key 'b'
  29. show create table t1;
  30. Table Create Table
  31. t1 CREATE TABLE `t1` (
  32. `a` int(11) NOT NULL,
  33. `b` int(11) DEFAULT NULL,
  34. `c` char(10) NOT NULL,
  35. `d` varchar(20) DEFAULT NULL,
  36. KEY `d2` (`d`)
  37. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  38. alter table t1 add index (b);
  39. show create table t1;
  40. Table Create Table
  41. t1 CREATE TABLE `t1` (
  42. `a` int(11) NOT NULL,
  43. `b` int(11) DEFAULT NULL,
  44. `c` char(10) NOT NULL,
  45. `d` varchar(20) DEFAULT NULL,
  46. KEY `d2` (`d`),
  47. KEY `b` (`b`)
  48. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  49. CREATE TABLE `t1#1`(a INT PRIMARY KEY) ENGINE=InnoDB;
  50. alter table t1 add unique index (c), add index (d);
  51. ERROR HY000: Table 'test.t1#1' already exists
  52. rename table `t1#1` to `t1#2`;
  53. alter table t1 add unique index (c), add index (d);
  54. ERROR HY000: Table 'test.t1#2' already exists
  55. drop table `t1#2`;
  56. alter table t1 add unique index (c), add index (d);
  57. show create table t1;
  58. Table Create Table
  59. t1 CREATE TABLE `t1` (
  60. `a` int(11) NOT NULL,
  61. `b` int(11) DEFAULT NULL,
  62. `c` char(10) NOT NULL,
  63. `d` varchar(20) DEFAULT NULL,
  64. UNIQUE KEY `c` (`c`),
  65. KEY `d2` (`d`),
  66. KEY `b` (`b`),
  67. KEY `d` (`d`)
  68. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  69. explain select * from t1 force index(c) order by c;
  70. id select_type table type possible_keys key key_len ref rows Extra
  71. 1 SIMPLE t1 index NULL c 10 NULL 4
  72. drop index c on t1;
  73. ERROR 42000: This table type requires a primary key
  74. alter table t1 add primary key (a), drop index c;
  75. show create table t1;
  76. Table Create Table
  77. t1 CREATE TABLE `t1` (
  78. `a` int(11) NOT NULL,
  79. `b` int(11) DEFAULT NULL,
  80. `c` char(10) NOT NULL,
  81. `d` varchar(20) DEFAULT NULL,
  82. PRIMARY KEY (`a`),
  83. KEY `d2` (`d`),
  84. KEY `b` (`b`),
  85. KEY `d` (`d`)
  86. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  87. alter table t1 add primary key (c);
  88. ERROR 42000: Multiple primary key defined
  89. alter table t1 drop primary key, add primary key (b);
  90. ERROR 23000: Duplicate entry '4' for key 'PRIMARY'
  91. create unique index c on t1 (c);
  92. show create table t1;
  93. Table Create Table
  94. t1 CREATE TABLE `t1` (
  95. `a` int(11) NOT NULL,
  96. `b` int(11) DEFAULT NULL,
  97. `c` char(10) NOT NULL,
  98. `d` varchar(20) DEFAULT NULL,
  99. PRIMARY KEY (`a`),
  100. UNIQUE KEY `c` (`c`),
  101. KEY `d2` (`d`),
  102. KEY `b` (`b`),
  103. KEY `d` (`d`)
  104. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  105. explain select * from t1 force index(c) order by c;
  106. id select_type table type possible_keys key key_len ref rows Extra
  107. 1 SIMPLE t1 index NULL c 10 NULL 4
  108. select * from t1 force index(c) order by c;
  109. a b c d
  110. 3 4 ad ad
  111. 2 3 ak ak
  112. 5 5 oo oo
  113. 4 4 tr tr
  114. alter table t1 drop index b, add index (b);
  115. show create table t1;
  116. Table Create Table
  117. t1 CREATE TABLE `t1` (
  118. `a` int(11) NOT NULL,
  119. `b` int(11) DEFAULT NULL,
  120. `c` char(10) NOT NULL,
  121. `d` varchar(20) DEFAULT NULL,
  122. PRIMARY KEY (`a`),
  123. UNIQUE KEY `c` (`c`),
  124. KEY `d2` (`d`),
  125. KEY `d` (`d`),
  126. KEY `b` (`b`)
  127. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  128. insert into t1 values(6,1,'ggg','ggg');
  129. select * from t1;
  130. a b c d
  131. 2 3 ak ak
  132. 3 4 ad ad
  133. 4 4 tr tr
  134. 5 5 oo oo
  135. 6 1 ggg ggg
  136. select * from t1 force index(b) order by b;
  137. a b c d
  138. 6 1 ggg ggg
  139. 2 3 ak ak
  140. 3 4 ad ad
  141. 4 4 tr tr
  142. 5 5 oo oo
  143. select * from t1 force index(c) order by c;
  144. a b c d
  145. 3 4 ad ad
  146. 2 3 ak ak
  147. 6 1 ggg ggg
  148. 5 5 oo oo
  149. 4 4 tr tr
  150. select * from t1 force index(d) order by d;
  151. a b c d
  152. 3 4 ad ad
  153. 2 3 ak ak
  154. 6 1 ggg ggg
  155. 5 5 oo oo
  156. 4 4 tr tr
  157. explain select * from t1 force index(b) order by b;
  158. id select_type table type possible_keys key key_len ref rows Extra
  159. 1 SIMPLE t1 index NULL b 5 NULL 5
  160. explain select * from t1 force index(c) order by c;
  161. id select_type table type possible_keys key key_len ref rows Extra
  162. 1 SIMPLE t1 index NULL c 10 NULL 5
  163. explain select * from t1 force index(d) order by d;
  164. id select_type table type possible_keys key key_len ref rows Extra
  165. 1 SIMPLE t1 index NULL d 23 NULL 5
  166. show create table t1;
  167. Table Create Table
  168. t1 CREATE TABLE `t1` (
  169. `a` int(11) NOT NULL,
  170. `b` int(11) DEFAULT NULL,
  171. `c` char(10) NOT NULL,
  172. `d` varchar(20) DEFAULT NULL,
  173. PRIMARY KEY (`a`),
  174. UNIQUE KEY `c` (`c`),
  175. KEY `d2` (`d`),
  176. KEY `d` (`d`),
  177. KEY `b` (`b`)
  178. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  179. drop table t1;
  180. create table t1(a int not null, b int, c char(10), d varchar(20), primary key (a)) engine = innodb;
  181. insert into t1 values (1,1,'ab','ab'),(2,2,'ac','ac'),(3,3,'ad','ad'),(4,4,'afe','afe');
  182. commit;
  183. alter table t1 add index (c(2));
  184. show create table t1;
  185. Table Create Table
  186. t1 CREATE TABLE `t1` (
  187. `a` int(11) NOT NULL,
  188. `b` int(11) DEFAULT NULL,
  189. `c` char(10) DEFAULT NULL,
  190. `d` varchar(20) DEFAULT NULL,
  191. PRIMARY KEY (`a`),
  192. KEY `c` (`c`(2))
  193. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  194. alter table t1 add unique index (d(10));
  195. show create table t1;
  196. Table Create Table
  197. t1 CREATE TABLE `t1` (
  198. `a` int(11) NOT NULL,
  199. `b` int(11) DEFAULT NULL,
  200. `c` char(10) DEFAULT NULL,
  201. `d` varchar(20) DEFAULT NULL,
  202. PRIMARY KEY (`a`),
  203. UNIQUE KEY `d` (`d`(10)),
  204. KEY `c` (`c`(2))
  205. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  206. insert into t1 values(5,1,'ggg','ggg');
  207. select * from t1;
  208. a b c d
  209. 1 1 ab ab
  210. 2 2 ac ac
  211. 3 3 ad ad
  212. 4 4 afe afe
  213. 5 1 ggg ggg
  214. select * from t1 force index(c) order by c;
  215. a b c d
  216. 1 1 ab ab
  217. 2 2 ac ac
  218. 3 3 ad ad
  219. 4 4 afe afe
  220. 5 1 ggg ggg
  221. select * from t1 force index(d) order by d;
  222. a b c d
  223. 1 1 ab ab
  224. 2 2 ac ac
  225. 3 3 ad ad
  226. 4 4 afe afe
  227. 5 1 ggg ggg
  228. explain select * from t1 order by b;
  229. id select_type table type possible_keys key key_len ref rows Extra
  230. 1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using filesort
  231. explain select * from t1 force index(c) order by c;
  232. id select_type table type possible_keys key key_len ref rows Extra
  233. 1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using filesort
  234. explain select * from t1 force index(d) order by d;
  235. id select_type table type possible_keys key key_len ref rows Extra
  236. 1 SIMPLE t1 ALL NULL NULL NULL NULL 5 Using filesort
  237. show create table t1;
  238. Table Create Table
  239. t1 CREATE TABLE `t1` (
  240. `a` int(11) NOT NULL,
  241. `b` int(11) DEFAULT NULL,
  242. `c` char(10) DEFAULT NULL,
  243. `d` varchar(20) DEFAULT NULL,
  244. PRIMARY KEY (`a`),
  245. UNIQUE KEY `d` (`d`(10)),
  246. KEY `c` (`c`(2))
  247. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  248. alter table t1 drop index d;
  249. insert into t1 values(8,9,'fff','fff');
  250. select * from t1;
  251. a b c d
  252. 1 1 ab ab
  253. 2 2 ac ac
  254. 3 3 ad ad
  255. 4 4 afe afe
  256. 5 1 ggg ggg
  257. 8 9 fff fff
  258. select * from t1 force index(c) order by c;
  259. a b c d
  260. 1 1 ab ab
  261. 2 2 ac ac
  262. 3 3 ad ad
  263. 4 4 afe afe
  264. 8 9 fff fff
  265. 5 1 ggg ggg
  266. explain select * from t1 order by b;
  267. id select_type table type possible_keys key key_len ref rows Extra
  268. 1 SIMPLE t1 ALL NULL NULL NULL NULL 6 Using filesort
  269. explain select * from t1 force index(c) order by c;
  270. id select_type table type possible_keys key key_len ref rows Extra
  271. 1 SIMPLE t1 ALL NULL NULL NULL NULL 6 Using filesort
  272. explain select * from t1 order by d;
  273. id select_type table type possible_keys key key_len ref rows Extra
  274. 1 SIMPLE t1 ALL NULL NULL NULL NULL 6 Using filesort
  275. show create table t1;
  276. Table Create Table
  277. t1 CREATE TABLE `t1` (
  278. `a` int(11) NOT NULL,
  279. `b` int(11) DEFAULT NULL,
  280. `c` char(10) DEFAULT NULL,
  281. `d` varchar(20) DEFAULT NULL,
  282. PRIMARY KEY (`a`),
  283. KEY `c` (`c`(2))
  284. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  285. drop table t1;
  286. create table t1(a int not null, b int, c char(10), d varchar(20), primary key (a)) engine = innodb;
  287. insert into t1 values (1,1,'ab','ab'),(2,2,'ac','ac'),(3,2,'ad','ad'),(4,4,'afe','afe');
  288. commit;
  289. alter table t1 add unique index (b,c);
  290. insert into t1 values(8,9,'fff','fff');
  291. select * from t1;
  292. a b c d
  293. 1 1 ab ab
  294. 2 2 ac ac
  295. 3 2 ad ad
  296. 4 4 afe afe
  297. 8 9 fff fff
  298. select * from t1 force index(b) order by b;
  299. a b c d
  300. 1 1 ab ab
  301. 2 2 ac ac
  302. 3 2 ad ad
  303. 4 4 afe afe
  304. 8 9 fff fff
  305. explain select * from t1 force index(b) order by b;
  306. id select_type table type possible_keys key key_len ref rows Extra
  307. 1 SIMPLE t1 index NULL b 16 NULL 5
  308. show create table t1;
  309. Table Create Table
  310. t1 CREATE TABLE `t1` (
  311. `a` int(11) NOT NULL,
  312. `b` int(11) DEFAULT NULL,
  313. `c` char(10) DEFAULT NULL,
  314. `d` varchar(20) DEFAULT NULL,
  315. PRIMARY KEY (`a`),
  316. UNIQUE KEY `b` (`b`,`c`)
  317. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  318. alter table t1 add index (b,c);
  319. insert into t1 values(11,11,'kkk','kkk');
  320. select * from t1;
  321. a b c d
  322. 1 1 ab ab
  323. 2 2 ac ac
  324. 3 2 ad ad
  325. 4 4 afe afe
  326. 8 9 fff fff
  327. 11 11 kkk kkk
  328. select * from t1 force index(b) order by b;
  329. a b c d
  330. 1 1 ab ab
  331. 2 2 ac ac
  332. 3 2 ad ad
  333. 4 4 afe afe
  334. 8 9 fff fff
  335. 11 11 kkk kkk
  336. explain select * from t1 force index(b) order by b;
  337. id select_type table type possible_keys key key_len ref rows Extra
  338. 1 SIMPLE t1 index NULL b 16 NULL 6
  339. show create table t1;
  340. Table Create Table
  341. t1 CREATE TABLE `t1` (
  342. `a` int(11) NOT NULL,
  343. `b` int(11) DEFAULT NULL,
  344. `c` char(10) DEFAULT NULL,
  345. `d` varchar(20) DEFAULT NULL,
  346. PRIMARY KEY (`a`),
  347. UNIQUE KEY `b` (`b`,`c`),
  348. KEY `b_2` (`b`,`c`)
  349. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  350. alter table t1 add unique index (c,d);
  351. insert into t1 values(13,13,'yyy','aaa');
  352. select * from t1;
  353. a b c d
  354. 1 1 ab ab
  355. 2 2 ac ac
  356. 3 2 ad ad
  357. 4 4 afe afe
  358. 8 9 fff fff
  359. 11 11 kkk kkk
  360. 13 13 yyy aaa
  361. select * from t1 force index(b) order by b;
  362. a b c d
  363. 1 1 ab ab
  364. 2 2 ac ac
  365. 3 2 ad ad
  366. 4 4 afe afe
  367. 8 9 fff fff
  368. 11 11 kkk kkk
  369. 13 13 yyy aaa
  370. select * from t1 force index(c) order by c;
  371. a b c d
  372. 1 1 ab ab
  373. 2 2 ac ac
  374. 3 2 ad ad
  375. 4 4 afe afe
  376. 8 9 fff fff
  377. 11 11 kkk kkk
  378. 13 13 yyy aaa
  379. explain select * from t1 force index(b) order by b;
  380. id select_type table type possible_keys key key_len ref rows Extra
  381. 1 SIMPLE t1 index NULL b 16 NULL 7
  382. explain select * from t1 force index(c) order by c;
  383. id select_type table type possible_keys key key_len ref rows Extra
  384. 1 SIMPLE t1 index NULL c 34 NULL 7
  385. show create table t1;
  386. Table Create Table
  387. t1 CREATE TABLE `t1` (
  388. `a` int(11) NOT NULL,
  389. `b` int(11) DEFAULT NULL,
  390. `c` char(10) DEFAULT NULL,
  391. `d` varchar(20) DEFAULT NULL,
  392. PRIMARY KEY (`a`),
  393. UNIQUE KEY `b` (`b`,`c`),
  394. UNIQUE KEY `c` (`c`,`d`),
  395. KEY `b_2` (`b`,`c`)
  396. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  397. drop table t1;
  398. create table t1(a int not null, b int not null, c int, primary key (a), key (b)) engine = innodb;
  399. create table t3(a int not null, c int not null, d int, primary key (a), key (c)) engine = innodb;
  400. create table t4(a int not null, d int not null, e int, primary key (a), key (d)) engine = innodb;
  401. create table t2(a int not null, b int not null, c int not null, d int not null, e int,
  402. foreign key (b) references t1(b) on delete cascade,
  403. foreign key (c) references t3(c), foreign key (d) references t4(d))
  404. engine = innodb;
  405. alter table t1 drop index b;
  406. ERROR HY000: Cannot drop index 'b': needed in a foreign key constraint
  407. alter table t3 drop index c;
  408. ERROR HY000: Cannot drop index 'c': needed in a foreign key constraint
  409. alter table t4 drop index d;
  410. ERROR HY000: Cannot drop index 'd': needed in a foreign key constraint
  411. alter table t2 drop index b;
  412. ERROR HY000: Cannot drop index 'b': needed in a foreign key constraint
  413. alter table t2 drop index b, drop index c, drop index d;
  414. ERROR HY000: Cannot drop index 'b': needed in a foreign key constraint
  415. create unique index dc on t2 (d,c);
  416. create index dc on t1 (b,c);
  417. alter table t2 add primary key (a);
  418. insert into t1 values (1,1,1);
  419. insert into t3 values (1,1,1);
  420. insert into t4 values (1,1,1);
  421. insert into t2 values (1,1,1,1,1);
  422. commit;
  423. alter table t4 add constraint dc foreign key (a) references t1(a);
  424. show create table t4;
  425. Table Create Table
  426. t4 CREATE TABLE `t4` (
  427. `a` int(11) NOT NULL,
  428. `d` int(11) NOT NULL,
  429. `e` int(11) DEFAULT NULL,
  430. PRIMARY KEY (`a`),
  431. KEY `d` (`d`),
  432. CONSTRAINT `dc` FOREIGN KEY (`a`) REFERENCES `t1` (`a`)
  433. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  434. alter table t3 add constraint dc foreign key (a) references t1(a);
  435. ERROR HY000: Can't create table '#sql-temporary' (errno: 121)
  436. show create table t3;
  437. Table Create Table
  438. t3 CREATE TABLE `t3` (
  439. `a` int(11) NOT NULL,
  440. `c` int(11) NOT NULL,
  441. `d` int(11) DEFAULT NULL,
  442. PRIMARY KEY (`a`),
  443. KEY `c` (`c`)
  444. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  445. alter table t2 drop index b, add index (b);
  446. show create table t2;
  447. Table Create Table
  448. t2 CREATE TABLE `t2` (
  449. `a` int(11) NOT NULL,
  450. `b` int(11) NOT NULL,
  451. `c` int(11) NOT NULL,
  452. `d` int(11) NOT NULL,
  453. `e` int(11) DEFAULT NULL,
  454. PRIMARY KEY (`a`),
  455. UNIQUE KEY `dc` (`d`,`c`),
  456. KEY `c` (`c`),
  457. KEY `b` (`b`),
  458. CONSTRAINT `t2_ibfk_1` FOREIGN KEY (`b`) REFERENCES `t1` (`b`) ON DELETE CASCADE,
  459. CONSTRAINT `t2_ibfk_2` FOREIGN KEY (`c`) REFERENCES `t3` (`c`),
  460. CONSTRAINT `t2_ibfk_3` FOREIGN KEY (`d`) REFERENCES `t4` (`d`)
  461. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  462. delete from t1;
  463. ERROR 23000: Cannot delete or update a parent row: a foreign key constraint fails (`test`.`t4`, CONSTRAINT `dc` FOREIGN KEY (`a`) REFERENCES `t1` (`a`))
  464. drop index dc on t4;
  465. ERROR 42000: Can't DROP 'dc'; check that column/key exists
  466. alter table t3 drop foreign key dc;
  467. ERROR HY000: Error on rename of './test/t3' to '#sql2-temporary' (errno: 152)
  468. alter table t4 drop foreign key dc;
  469. select * from t2;
  470. a b c d e
  471. 1 1 1 1 1
  472. delete from t1;
  473. select * from t2;
  474. a b c d e
  475. drop table t2,t4,t3,t1;
  476. create table t1(a int not null, b int, c char(10), d varchar(20), primary key (a)) engine = innodb default charset=utf8;
  477. insert into t1 values (1,1,'ab','ab'),(2,2,'ac','ac'),(3,2,'ad','ad'),(4,4,'afe','afe');
  478. commit;
  479. alter table t1 add unique index (b);
  480. ERROR 23000: Duplicate entry '2' for key 'b'
  481. insert into t1 values(8,9,'fff','fff');
  482. select * from t1;
  483. a b c d
  484. 1 1 ab ab
  485. 2 2 ac ac
  486. 3 2 ad ad
  487. 4 4 afe afe
  488. 8 9 fff fff
  489. show create table t1;
  490. Table Create Table
  491. t1 CREATE TABLE `t1` (
  492. `a` int(11) NOT NULL,
  493. `b` int(11) DEFAULT NULL,
  494. `c` char(10) DEFAULT NULL,
  495. `d` varchar(20) DEFAULT NULL,
  496. PRIMARY KEY (`a`)
  497. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  498. alter table t1 add index (b);
  499. insert into t1 values(10,10,'kkk','iii');
  500. select * from t1;
  501. a b c d
  502. 1 1 ab ab
  503. 2 2 ac ac
  504. 3 2 ad ad
  505. 4 4 afe afe
  506. 8 9 fff fff
  507. 10 10 kkk iii
  508. select * from t1 force index(b) order by b;
  509. a b c d
  510. 1 1 ab ab
  511. 2 2 ac ac
  512. 3 2 ad ad
  513. 4 4 afe afe
  514. 8 9 fff fff
  515. 10 10 kkk iii
  516. explain select * from t1 force index(b) order by b;
  517. id select_type table type possible_keys key key_len ref rows Extra
  518. 1 SIMPLE t1 index NULL b 5 NULL 6
  519. show create table t1;
  520. Table Create Table
  521. t1 CREATE TABLE `t1` (
  522. `a` int(11) NOT NULL,
  523. `b` int(11) DEFAULT NULL,
  524. `c` char(10) DEFAULT NULL,
  525. `d` varchar(20) DEFAULT NULL,
  526. PRIMARY KEY (`a`),
  527. KEY `b` (`b`)
  528. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  529. alter table t1 add unique index (c), add index (d);
  530. insert into t1 values(11,11,'aaa','mmm');
  531. select * from t1;
  532. a b c d
  533. 1 1 ab ab
  534. 2 2 ac ac
  535. 3 2 ad ad
  536. 4 4 afe afe
  537. 8 9 fff fff
  538. 10 10 kkk iii
  539. 11 11 aaa mmm
  540. select * from t1 force index(b) order by b;
  541. a b c d
  542. 1 1 ab ab
  543. 2 2 ac ac
  544. 3 2 ad ad
  545. 4 4 afe afe
  546. 8 9 fff fff
  547. 10 10 kkk iii
  548. 11 11 aaa mmm
  549. select * from t1 force index(c) order by c;
  550. a b c d
  551. 11 11 aaa mmm
  552. 1 1 ab ab
  553. 2 2 ac ac
  554. 3 2 ad ad
  555. 4 4 afe afe
  556. 8 9 fff fff
  557. 10 10 kkk iii
  558. select * from t1 force index(d) order by d;
  559. a b c d
  560. 1 1 ab ab
  561. 2 2 ac ac
  562. 3 2 ad ad
  563. 4 4 afe afe
  564. 8 9 fff fff
  565. 10 10 kkk iii
  566. 11 11 aaa mmm
  567. explain select * from t1 force index(b) order by b;
  568. id select_type table type possible_keys key key_len ref rows Extra
  569. 1 SIMPLE t1 index NULL b 5 NULL 7
  570. explain select * from t1 force index(c) order by c;
  571. id select_type table type possible_keys key key_len ref rows Extra
  572. 1 SIMPLE t1 index NULL c 31 NULL 7
  573. explain select * from t1 force index(d) order by d;
  574. id select_type table type possible_keys key key_len ref rows Extra
  575. 1 SIMPLE t1 index NULL d 63 NULL 7
  576. show create table t1;
  577. Table Create Table
  578. t1 CREATE TABLE `t1` (
  579. `a` int(11) NOT NULL,
  580. `b` int(11) DEFAULT NULL,
  581. `c` char(10) DEFAULT NULL,
  582. `d` varchar(20) DEFAULT NULL,
  583. PRIMARY KEY (`a`),
  584. UNIQUE KEY `c` (`c`),
  585. KEY `b` (`b`),
  586. KEY `d` (`d`)
  587. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  588. check table t1;
  589. Table Op Msg_type Msg_text
  590. test.t1 check status OK
  591. drop table t1;
  592. create table t1(a int not null, b int) engine = innodb;
  593. insert into t1 values (1,1),(1,1),(1,1),(1,1);
  594. alter table t1 add unique index (a);
  595. ERROR 23000: Duplicate entry '1' for key 'a'
  596. alter table t1 add unique index (b);
  597. ERROR 23000: Duplicate entry '1' for key 'b'
  598. alter table t1 add unique index (a), add unique index(b);
  599. ERROR 23000: Duplicate entry '1' for key 'a'
  600. show create table t1;
  601. Table Create Table
  602. t1 CREATE TABLE `t1` (
  603. `a` int(11) NOT NULL,
  604. `b` int(11) DEFAULT NULL
  605. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  606. drop table t1;
  607. create table t1(a int not null, c int not null,b int, primary key(a), unique key(c), key(b)) engine = innodb;
  608. alter table t1 drop index c, drop index b;
  609. show create table t1;
  610. Table Create Table
  611. t1 CREATE TABLE `t1` (
  612. `a` int(11) NOT NULL,
  613. `c` int(11) NOT NULL,
  614. `b` int(11) DEFAULT NULL,
  615. PRIMARY KEY (`a`)
  616. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  617. drop table t1;
  618. create table t1(a int not null, b int, primary key(a)) engine = innodb;
  619. alter table t1 add index (b);
  620. show create table t1;
  621. Table Create Table
  622. t1 CREATE TABLE `t1` (
  623. `a` int(11) NOT NULL,
  624. `b` int(11) DEFAULT NULL,
  625. PRIMARY KEY (`a`),
  626. KEY `b` (`b`)
  627. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  628. drop table t1;
  629. create table t1(a int not null, b int, c char(10), d varchar(20), primary key (a)) engine = innodb;
  630. insert into t1 values (1,1,'ab','ab'),(2,2,'ac','ac'),(3,3,'ac','ac'),(4,4,'afe','afe');
  631. alter table t1 add unique index (b), add unique index (c), add unique index (d);
  632. ERROR 23000: Duplicate entry 'ac' for key 'c'
  633. alter table t1 add unique index (b), add index (d), add unique index (c);
  634. ERROR 23000: Duplicate entry 'ac' for key 'c'
  635. show create table t1;
  636. Table Create Table
  637. t1 CREATE TABLE `t1` (
  638. `a` int(11) NOT NULL,
  639. `b` int(11) DEFAULT NULL,
  640. `c` char(10) DEFAULT NULL,
  641. `d` varchar(20) DEFAULT NULL,
  642. PRIMARY KEY (`a`)
  643. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  644. drop table t1;
  645. create table t1(a int not null, b int not null, c int, primary key (a), key(c)) engine=innodb;
  646. insert into t1 values (5,1,5),(4,2,4),(3,3,3),(2,4,2),(1,5,1);
  647. alter table t1 add unique index (b);
  648. insert into t1 values (10,20,20),(11,19,19),(12,18,18),(13,17,17);
  649. show create table t1;
  650. Table Create Table
  651. t1 CREATE TABLE `t1` (
  652. `a` int(11) NOT NULL,
  653. `b` int(11) NOT NULL,
  654. `c` int(11) DEFAULT NULL,
  655. PRIMARY KEY (`a`),
  656. UNIQUE KEY `b` (`b`),
  657. KEY `c` (`c`)
  658. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  659. check table t1;
  660. Table Op Msg_type Msg_text
  661. test.t1 check status OK
  662. explain select * from t1 force index(c) order by c;
  663. id select_type table type possible_keys key key_len ref rows Extra
  664. 1 SIMPLE t1 index NULL c 5 NULL 9
  665. explain select * from t1 order by a;
  666. id select_type table type possible_keys key key_len ref rows Extra
  667. 1 SIMPLE t1 index NULL PRIMARY 4 NULL 9
  668. explain select * from t1 force index(b) order by b;
  669. id select_type table type possible_keys key key_len ref rows Extra
  670. 1 SIMPLE t1 index NULL b 4 NULL 9
  671. select * from t1 order by a;
  672. a b c
  673. 1 5 1
  674. 2 4 2
  675. 3 3 3
  676. 4 2 4
  677. 5 1 5
  678. 10 20 20
  679. 11 19 19
  680. 12 18 18
  681. 13 17 17
  682. select * from t1 force index(b) order by b;
  683. a b c
  684. 5 1 5
  685. 4 2 4
  686. 3 3 3
  687. 2 4 2
  688. 1 5 1
  689. 13 17 17
  690. 12 18 18
  691. 11 19 19
  692. 10 20 20
  693. select * from t1 force index(c) order by c;
  694. a b c
  695. 1 5 1
  696. 2 4 2
  697. 3 3 3
  698. 4 2 4
  699. 5 1 5
  700. 13 17 17
  701. 12 18 18
  702. 11 19 19
  703. 10 20 20
  704. drop table t1;
  705. create table t1(a int not null, b int not null) engine=innodb;
  706. insert into t1 values (1,1);
  707. alter table t1 add primary key(b);
  708. insert into t1 values (2,2);
  709. show create table t1;
  710. Table Create Table
  711. t1 CREATE TABLE `t1` (
  712. `a` int(11) NOT NULL,
  713. `b` int(11) NOT NULL,
  714. PRIMARY KEY (`b`)
  715. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  716. check table t1;
  717. Table Op Msg_type Msg_text
  718. test.t1 check status OK
  719. select * from t1;
  720. a b
  721. 1 1
  722. 2 2
  723. explain select * from t1;
  724. id select_type table type possible_keys key key_len ref rows Extra
  725. 1 SIMPLE t1 ALL NULL NULL NULL NULL 2
  726. explain select * from t1 order by a;
  727. id select_type table type possible_keys key key_len ref rows Extra
  728. 1 SIMPLE t1 ALL NULL NULL NULL NULL 2 Using filesort
  729. explain select * from t1 order by b;
  730. id select_type table type possible_keys key key_len ref rows Extra
  731. 1 SIMPLE t1 index NULL PRIMARY 4 NULL 2
  732. checksum table t1;
  733. Table Checksum
  734. test.t1 582702641
  735. drop table t1;
  736. create table t1(a int not null) engine=innodb;
  737. insert into t1 values (1);
  738. alter table t1 add primary key(a);
  739. insert into t1 values (2);
  740. show create table t1;
  741. Table Create Table
  742. t1 CREATE TABLE `t1` (
  743. `a` int(11) NOT NULL,
  744. PRIMARY KEY (`a`)
  745. ) ENGINE=InnoDB DEFAULT CHARSET=latin1
  746. check table t1;
  747. Table Op Msg_type Msg_text
  748. test.t1 check status OK
  749. commit;
  750. select * from t1;
  751. a
  752. 1
  753. 2
  754. explain select * from t1;
  755. id select_type table type possible_keys key key_len ref rows Extra
  756. 1 SIMPLE t1 index NULL PRIMARY 4 NULL 2 Using index
  757. explain select * from t1 order by a;
  758. id select_type table type possible_keys key key_len ref rows Extra
  759. 1 SIMPLE t1 index NULL PRIMARY 4 NULL 2 Using index
  760. drop table t1;
  761. create table t2(d varchar(17) primary key) engine=innodb default charset=utf8;
  762. create table t3(a int primary key) engine=innodb;
  763. insert into t3 values(22),(44),(33),(55),(66);
  764. insert into t2 values ('jejdkrun87'),('adfd72nh9k'),
  765. ('adfdpplkeock'),('adfdijnmnb78k'),('adfdijn0loKNHJik');
  766. create table t1(a int, b blob, c text, d text not null)
  767. engine=innodb default charset = utf8;
  768. insert into t1 values (null,null,null,'null');
  769. insert into t1
  770. select a,left(repeat(d,100*a),65535),repeat(d,20*a),d from t2,t3;
  771. drop table t2, t3;
  772. select count(*) from t1 where a=44;
  773. count(*)
  774. 5
  775. select a,
  776. length(b),b=left(repeat(d,100*a),65535),length(c),c=repeat(d,20*a),d from t1;
  777. a length(b) b=left(repeat(d,100*a),65535) length(c) c=repeat(d,20*a) d
  778. NULL NULL NULL NULL NULL null
  779. 22 22000 1 4400 1 adfd72nh9k
  780. 22 35200 1 7040 1 adfdijn0loKNHJik
  781. 22 28600 1 5720 1 adfdijnmnb78k
  782. 22 26400 1 5280 1 adfdpplkeock
  783. 22 22000 1 4400 1 jejdkrun87
  784. 33 33000 1 6600 1 adfd72nh9k
  785. 33 52800 1 10560 1 adfdijn0loKNHJik
  786. 33 42900 1 8580 1 adfdijnmnb78k
  787. 33 39600 1 7920 1 adfdpplkeock
  788. 33 33000 1 6600 1 jejdkrun87
  789. 44 44000 1 8800 1 adfd72nh9k
  790. 44 65535 1 14080 1 adfdijn0loKNHJik
  791. 44 57200 1 11440 1 adfdijnmnb78k
  792. 44 52800 1 10560 1 adfdpplkeock
  793. 44 44000 1 8800 1 jejdkrun87
  794. 55 55000 1 11000 1 adfd72nh9k
  795. 55 65535 1 17600 1 adfdijn0loKNHJik
  796. 55 65535 1 14300 1 adfdijnmnb78k
  797. 55 65535 1 13200 1 adfdpplkeock
  798. 55 55000 1 11000 1 jejdkrun87
  799. 66 65535 1 13200 1 adfd72nh9k
  800. 66 65535 1 21120 1 adfdijn0loKNHJik
  801. 66 65535 1 17160 1 adfdijnmnb78k
  802. 66 65535 1 15840 1 adfdpplkeock
  803. 66 65535 1 13200 1 jejdkrun87
  804. alter table t1 add primary key (a), add key (b(20));
  805. ERROR 42000: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead
  806. delete from t1 where d='null';
  807. alter table t1 add primary key (a), add key (b(20));
  808. ERROR 23000: Duplicate entry '22' for key 'PRIMARY'
  809. delete from t1 where a%2;
  810. check table t1;
  811. Table Op Msg_type Msg_text
  812. test.t1 check status OK
  813. alter table t1 add primary key (a,b(255),c(255)), add key (b(767));
  814. select count(*) from t1 where a=44;
  815. count(*)
  816. 5
  817. select a,
  818. length(b),b=left(repeat(d,100*a),65535),length(c),c=repeat(d,20*a),d from t1;
  819. a length(b) b=left(repeat(d,100*a),65535) length(c) c=repeat(d,20*a) d
  820. 22 22000 1 4400 1 adfd72nh9k
  821. 22 35200 1 7040 1 adfdijn0loKNHJik
  822. 22 28600 1 5720 1 adfdijnmnb78k
  823. 22 26400 1 5280 1 adfdpplkeock
  824. 22 22000 1 4400 1 jejdkrun87
  825. 44 44000 1 8800 1 adfd72nh9k
  826. 44 65535 1 14080 1 adfdijn0loKNHJik
  827. 44 57200 1 11440 1 adfdijnmnb78k
  828. 44 52800 1 10560 1 adfdpplkeock
  829. 44 44000 1 8800 1 jejdkrun87
  830. 66 65535 1 13200 1 adfd72nh9k
  831. 66 65535 1 21120 1 adfdijn0loKNHJik
  832. 66 65535 1 17160 1 adfdijnmnb78k
  833. 66 65535 1 15840 1 adfdpplkeock
  834. 66 65535 1 13200 1 jejdkrun87
  835. show create table t1;
  836. Table Create Table
  837. t1 CREATE TABLE `t1` (
  838. `a` int(11) NOT NULL DEFAULT '0',
  839. `b` blob NOT NULL,
  840. `c` text NOT NULL,
  841. `d` text NOT NULL,
  842. PRIMARY KEY (`a`,`b`(255),`c`(255)),
  843. KEY `b` (`b`(767))
  844. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  845. check table t1;
  846. Table Op Msg_type Msg_text
  847. test.t1 check status OK
  848. explain select * from t1 where b like 'adfd%';
  849. id select_type table type possible_keys key key_len ref rows Extra
  850. 1 SIMPLE t1 range b b 769 NULL 11 Using where
  851. begin;
  852. select a from t1 limit 1 for update;
  853. a
  854. 22
  855. create index t1ba on t1 (b(10),a);
  856. ERROR HY000: Lock wait timeout exceeded; try restarting transaction
  857. commit;
  858. begin;
  859. select a from t1 limit 1 lock in share mode;
  860. a
  861. 22
  862. create index t1ba on t1 (b(10),a);
  863. drop index t1ba on t1;
  864. ERROR HY000: Lock wait timeout exceeded; try restarting transaction
  865. commit;
  866. explain select a from t1 order by b;
  867. id select_type table type possible_keys key key_len ref rows Extra
  868. 1 SIMPLE t1 index NULL t1ba 16 NULL 60 Using index; Using filesort
  869. select a,sleep(2+a/100) from t1 order by b limit 3;
  870. select sleep(1);
  871. sleep(1)
  872. 0
  873. drop index t1ba on t1;
  874. a sleep(2+a/100)
  875. 22 0
  876. 44 0
  877. 66 0
  878. explain select a from t1 order by b;
  879. id select_type table type possible_keys key key_len ref rows Extra
  880. 1 SIMPLE t1 index NULL PRIMARY 1028 NULL 60 Using index; Using filesort
  881. select a from t1 order by b limit 3;
  882. a
  883. 22
  884. 44
  885. 66
  886. commit;
  887. drop table t1;
  888. create table t1(a blob,b blob,c blob,d blob,e blob,f blob,g blob,h blob,
  889. i blob,j blob,k blob,l blob,m blob,n blob,o blob,p blob,
  890. q blob,r blob,s blob,t blob,u blob)
  891. engine=innodb;
  892. create index t1a on t1 (a(1));
  893. create index t1b on t1 (b(1));
  894. create index t1c on t1 (c(1));
  895. create index t1d on t1 (d(1));
  896. create index t1e on t1 (e(1));
  897. create index t1f on t1 (f(1));
  898. create index t1g on t1 (g(1));
  899. create index t1h on t1 (h(1));
  900. create index t1i on t1 (i(1));
  901. create index t1j on t1 (j(1));
  902. create index t1k on t1 (k(1));
  903. create index t1l on t1 (l(1));
  904. create index t1m on t1 (m(1));
  905. create index t1n on t1 (n(1));
  906. create index t1o on t1 (o(1));
  907. create index t1p on t1 (p(1));
  908. create index t1q on t1 (q(1));
  909. create index t1r on t1 (r(1));
  910. create index t1s on t1 (s(1));
  911. create index t1t on t1 (t(1));
  912. create index t1u on t1 (u(1));
  913. ERROR HY000: Too big row
  914. create index t1ut on t1 (u(1), t(1));
  915. ERROR HY000: Too big row
  916. create index t1st on t1 (s(1), t(1));
  917. show create table t1;
  918. Table Create Table
  919. t1 CREATE TABLE `t1` (
  920. `a` blob,
  921. `b` blob,
  922. `c` blob,
  923. `d` blob,
  924. `e` blob,
  925. `f` blob,
  926. `g` blob,
  927. `h` blob,
  928. `i` blob,
  929. `j` blob,
  930. `k` blob,
  931. `l` blob,
  932. `m` blob,
  933. `n` blob,
  934. `o` blob,
  935. `p` blob,
  936. `q` blob,
  937. `r` blob,
  938. `s` blob,
  939. `t` blob,
  940. `u` blob,
  941. KEY `t1a` (`a`(1)),
  942. KEY `t1b` (`b`(1)),
  943. KEY `t1c` (`c`(1)),
  944. KEY `t1d` (`d`(1)),
  945. KEY `t1e` (`e`(1)),
  946. KEY `t1f` (`f`(1)),
  947. KEY `t1g` (`g`(1)),
  948. KEY `t1h` (`h`(1)),
  949. KEY `t1i` (`i`(1)),
  950. KEY `t1j` (`j`(1)),
  951. KEY `t1k` (`k`(1)),
  952. KEY `t1l` (`l`(1)),
  953. KEY `t1m` (`m`(1)),
  954. KEY `t1n` (`n`(1)),
  955. KEY `t1o` (`o`(1)),
  956. KEY `t1p` (`p`(1)),
  957. KEY `t1q` (`q`(1)),
  958. KEY `t1r` (`r`(1)),
  959. KEY `t1s` (`s`(1)),
  960. KEY `t1t` (`t`(1)),
  961. KEY `t1st` (`s`(1),`t`(1))
  962. ) ENGINE=InnoDB DEFAULT CHARSET=latin1