index) some times, but usually much more slower than composite index, or when mysql choose not to use it; Index WILL NOT HELP if the index cover too large part of the table (sequential scan is faster) Thursday, 17 May, 12
ORDER BY created_at LIMIT 25 OFFSET 0 *** If tbl is too large, LIMIT or ORDER BY won’t help, MySQL still need to examined all rows Solution: Make rows examined smaller! (with where conditions, for example, why use LIMIT instead of > on created_at?) Thursday, 17 May, 12
last_name CHAR(30) NOT NULL, first_name CHAR(30) NOT NULL, PRIMARY KEY (id), INDEX name (last_name,first_name) ); Leftmost prefix of index: last_name > first_name Thursday, 17 May, 12
test WHERE last_name='Widenius'; SELECT * FROM test WHERE last_name='Widenius' AND first_name='Michael'; SELECT * FROM test WHERE last_name='Widenius' AND (first_name='Michael' OR first_name='Monty'); SELECT * FROM test WHERE last_name='Widenius' AND first_name >='M' AND first_name < 'N'; Thursday, 17 May, 12
BY key_part1,key_part2,... ; SELECT * FROM t1 WHERE key_part1=constant ORDER BY key_part2; SELECT * FROM t1 ORDER BY key_part1 DESC, key_part2 DESC; SELECT * FROM t1 WHERE key_part1=1 ORDER BY key_part1 DESC, key_part2 DESC; SELECT * FROM activities WHERE user_id=1234 ORDER BY created_at; Thursday, 17 May, 12
NOT NULL AUTO_INCREMENT PRIMARY KEY, id BINARY(16) NOT NULL, updated TIMESTAMP NOT NULL, body MEDIUMBLOB, UNIQUE KEY (id), KEY (updated) ) ENGINE=InnoDB; Thursday, 17 May, 12
NOT NULL AUTO_INCREMENT PRIMARY KEY, id BINARY(16) NOT NULL, updated TIMESTAMP NOT NULL, body MEDIUMBLOB, UNIQUE KEY (id), KEY (updated) ) ENGINE=InnoDB; InnoDB stores data rows physically in primary key order Ensures new entities are written sequentially on disk after old entities, which helps for both read and write locality (new entities tend to be read more frequently than old entities) Thursday, 17 May, 12
Lock=0.00s (0s) Rows=13.7 (466), artstack[artstack]@localhost SELECT `activities`.* FROM `activities` WHERE `activities`.`user_id` = N ORDER BY created_at DESC LIMIT N OFFSET N Count: 43 Time=54.65s (2350s) Lock=0.00s (0s) Rows=1.0 (43), artstack[artstack]@localhost SELECT COUNT(count_column) FROM (SELECT N AS count_column FROM `activities` WHERE `activities`.`user_id` = N LIMIT N OFFSET N) subquery_for_count Count: 1 Time=4.42s (4s) Lock=0.00s (0s) Rows=25.0 (25), artstack[artstack]@localhost SELECT `activities`.user_id FROM `activities` WHERE `activities`.`user_id` = N ORDER BY created_at DESC LIMIT N OFFSET N Count: 1 Time=2.02s (2s) Lock=0.00s (0s) Rows=6.0 (6), artstack[artstack]@localhost SELECT `users`.* FROM `users` INNER JOIN `followings` ON `users`.`id` = `followings`.`follower_id` WHERE `followings`.`followable_id` = N AND `followings`.`followable_type` = 'S' ORDER BY rand() LIMIT N Count: 1 Time=1.90s (1s) Lock=0.00s (0s) Rows=25.0 (25), artstack[artstack]@localhost SELECT `activities`.* FROM `activities` WHERE `activities`.`user_id` = N order by id desc LIMIT N OFFSET N Thursday, 17 May, 12