mysql large table paging query test analysis optimization

Index concept:
http://blog.csdn.net/xluren/article/details/32746183
http://www.cnblogs.com/kupig/archive/2011/10/19/2217228.html
http://blog.csdn .net/longyulu/article/details/8850672
First create the table structure
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `age` int(11) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

8 million data in and out; the
test starts:
1. Normal query
SELECT * FROM test.`user` LIMIT 7921323,10

Time: 3.513s
2. Sort by id
SELECT * FROM test.`user` ORDER BY id LIMIT 7721323, 10;

Time consuming 3.408s
3. Use IN
SELECT * FROM test.`user` WHERE id IN (7921323,7921300,7921386)

Time-consuming: 0.097s, this is mainly because the index is used, so the speed is fast
Let take a look at the situation without index
SELECT * FROM test.`user` WHERE `name` IN ('jack7999008','jack7699008','jack7599008')

Time-consuming: 3.683s
For the case of IN, it is necessary to find the ID. For millions of tables, it takes time to query the ID. For the conditional paging query, we can build an index for the field corresponding to the condition to find the ID. , and then IN SET;
4. The use Id mentioned on the Internet
SELECT * FROM test.`user` WHERE id >= (SELECT id FROM test.`user` LIMIT 7721323, 1) LIMIT 10;

Time: 3.409s
5.BETWEEN
SELECT * FROM test.`user` WHERE  id BETWEEN 7921323 AND 7921333  and `name` LIKE '%jack%' ;

It takes 0.001s, this case also uses the primary key index
SELECT * FROM test.`user` WHERE `name` LIKE '%jack%' AND id BETWEEN 7921323 AND 7921333

It takes 0.160s. From this, it can be seen that there is a difference between the conditions before and after;
6. Simply querying a record without using an index
SELECT * FROM test.`user` WHERE `name` LIKE 'jack7721323'
takes 3.777s
Add an index to the name field
ALTER TABLE user ADD INDEX(name);
then execute the above query:
time-consuming 0.258s
show index from user; view user table index
show table status from test where name='user'; view user table status
Summary :
For continuous IDs, we can use BETWEEN; for discontinuous IDs, we can use IN, but this requires querying the ID set. We can add INDEX to the conditions to be queried. It should be noted that MySQL is only suitable for the following operators. Use indexes, <,<=,=,>,>=, between, in, and sometimes like (not starting with wildcard % or _), and theoretically, up to 16 indexes can be created in each table , but unless the amount of data is really large, if the index is used too much, the more fields to be indexed, and when the amount of data is large, the index file will be larger, which is the most significant problem. At the same time
, an index An I/O will be added during update or insert, which is very performance-intensive for the bottom layer of the operating system. If there are too many indexes, the index file will be too large (exponential growth), and the query time of the system will increase during addressing.






Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326711441&siteId=291194637