You need to use index to select multiple rows effectively. Let us first create a table −
mysql> create table DemoTable1501 -> ( -> Id int NOT NULL PRIMARY KEY, -> URL text -> ); Query OK, 0 rows affected (0.62 sec)
Here is the query to create index −
mysql> create index id_index on DemoTable1501(Id); Query OK, 0 rows affected (0.23 sec) Records: 0 Duplicates: 0 Warnings: 0
Insert some records in the table using insert command −
mysql> insert into DemoTable1501 values(101,'www.facebook.com'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1501 values(110,'www.google.com'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1501 values(220,'www.gmail.com'); Query OK, 1 row affected (0.06 sec) mysql> insert into DemoTable1501 values(350,'www.youtube.com'); Query OK, 1 row affected (0.08 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1501;
This will produce the following output −
+-----+------------------+ | Id | URL | +-----+------------------+ | 101 | www.facebook.com | | 110 | www.google.com | | 220 | www.gmail.com | | 350 | www.youtube.com | +-----+------------------+ 4 rows in set (0.00 sec)
Following is the query to select multiple rows efficiently −
mysql> select * from DemoTable1501 -> where Id in(101,220,350);
This will produce the following output −
+-----+------------------+ | Id | URL | +-----+------------------+ | 101 | www.facebook.com | | 220 | www.gmail.com | | 350 | www.youtube.com | +-----+------------------+ 3 rows in set (0.00 sec)
To prove this, use the SHOW command in which the Handler_read_key uses 3 out of 4 Ids −
mysql> SHOW STATUS LIKE 'Handler_%';
This will produce the following output −
+----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | Handler_commit | 1 | | Handler_delete | 0 | | Handler_discover | 0 | | Handler_external_lock | 2 | | Handler_mrr_init | 0 | | Handler_prepare | 0 | | Handler_read_first | 0 | | Handler_read_key | 3 | | Handler_read_last | 0 | | Handler_read_next | 0 | | Handler_read_prev | 0 | | Handler_read_rnd | 0 | | Handler_read_rnd_next | 0 | | Handler_rollback | 0 | | Handler_savepoint | 0 | | Handler_savepoint_rollback | 0 | | Handler_update | 0 | | Handler_write | 0 | +----------------------------+-------+ 18 rows in set (0.00 sec)