简体   繁体   中英

MySQL - Selecting Multiple rows effectively

Lets Say i have a table like this

WEB_LIST_TABLE

     KEY             Value
----------------------------------------
     134             google.com
     187             yahoo.com
     353             facebook.com

So on may be a million rows

The keys are primary keys and if i want to Select 50 Rows ,using the key, such as

Select * from WEB_LIST_TABLE where KEY in (134,187,...);

I m using where KEY in for selecting the rows, is it effective, based on performance ..?

Is there any other effective ways to do this?

This is effective, since field key is indexed. MySQL will use index to get the desired rows.

mysql>FLUSH STATUS;
mysql>SELECT * FROM WEB_LIST_TABLE WHERE KEY IN (134,187,192,241,10);
...
mysql>SHOW STATUS LIKE 'Handler_%';
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| Handler_read_first         | 0     |
| Handler_read_key           | 5     |
| Handler_read_next          | 0     |
| Handler_read_prev          | 0     |
| Handler_read_rnd           | 0     |
| Handler_read_rnd_next      | 0     |
+----------------------------+-------+

As you can see, Handler_read_key is 5.

According to documentation this means that it read all the five rows based on key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM