mysqlスロークエリー最適化------limitが使用可能な場合はlimitを使用します
6860 ワード
多くの場合、クエリーの結果は最大1つのレコードデータであると予想されています.この場合、limit 1を使用したほうがいいです.このデータを検索すると、mysqlはすぐにクエリーの継続を終了し、より多くの不要なクエリーを行わずに効率を向上させます.
実際にテストしてみましょう.10万ドルのmysqlテーブルで、lilyのスコアを検索します(システムに1つのlilyしかないと仮定しますが、私たちもこのデータしか必要ないと予想しています).時間の違いを示すために、テーブルのnameフィールドにインデックスを作成しません.
まず、テーブル構造を見てみましょう.
set profiling=1を開きます.のスイッチでmysql文を実行して比較します.
私たちはすでに理論と実践のシナリオからlimitの長所を説明しているので、limitが利用できるときにlimitを使うことをお勧めします(もちろん、結果が複数であれば、limit 1は絶対にできませんよ)
実際にテストしてみましょう.10万ドルのmysqlテーブルで、lilyのスコアを検索します(システムに1つのlilyしかないと仮定しますが、私たちもこのデータしか必要ないと予想しています).時間の違いを示すために、テーブルのnameフィールドにインデックスを作成しません.
まず、テーブル構造を見てみましょう.
mysql> show create table tb_province;
+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_province | CREATE TABLE `tb_province` (
`id` bigint(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
`score` int(10) unsigned DEFAULT '0',
`x` int(10) unsigned DEFAULT '0',
`x1` int(10) unsigned DEFAULT '0',
`x2` int(10) unsigned DEFAULT '0',
`x3` int(10) unsigned DEFAULT '0',
`x4` int(10) unsigned DEFAULT '0',
`x5` int(10) unsigned DEFAULT '0',
`x6` int(10) unsigned DEFAULT '0',
`x7` int(10) unsigned DEFAULT '0',
`x8` int(10) unsigned DEFAULT '0',
`x9` int(10) unsigned DEFAULT '0',
`x10` int(10) unsigned DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=124178 DEFAULT CHARSET=latin1 |
+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
set profiling=1を開きます.のスイッチでmysql文を実行して比較します.
mysql> select score from tb_province where name='lily';
+-------+
| score |
+-------+
| 100 |
+-------+
1 row in set (0.03 sec)
mysql> select score from tb_province where name='lily';
+-------+
| score |
+-------+
| 100 |
+-------+
1 row in set (0.03 sec)
mysql> select score from tb_province where name='lily';
+-------+
| score |
+-------+
| 100 |
+-------+
1 row in set (0.04 sec)
mysql> select score from tb_province where name='lily';
+-------+
| score |
+-------+
| 100 |
+-------+
1 row in set (0.02 sec)
mysql> select score from tb_province where name='lily';
+-------+
| score |
+-------+
| 100 |
+-------+
1 row in set (0.03 sec)
mysql> select score from tb_province where name='lily' limit 1;
+-------+
| score |
+-------+
| 100 |
+-------+
1 row in set (0.00 sec)
mysql> select score from tb_province where name='lily' limit 1;
+-------+
| score |
+-------+
| 100 |
+-------+
1 row in set (0.00 sec)
mysql> select score from tb_province where name='lily' limit 1;
+-------+
| score |
+-------+
| 100 |
+-------+
1 row in set (0.00 sec)
mysql> select score from tb_province where name='lily' limit 1;
+-------+
| score |
+-------+
| 100 |
+-------+
1 row in set (0.01 sec)
mysql> select score from tb_province where name='lily' limit 1;
+-------+
| score |
+-------+
| 100 |
+-------+
1 row in set (0.00 sec)
に示すように、limit 1を採用するかどうかについて5回の比較テストを行い、結果を見てみましょう.mysql> show profiles;
+----------+------------+---------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+---------------------------------------------------------+
| 5 | 0.02686000 | select score from tb_province where name='lily' |
| 6 | 0.02649050 | select score from tb_province where name='lily' |
| 7 | 0.03413500 | select score from tb_province where name='lily' |
| 8 | 0.02601350 | select score from tb_province where name='lily' |
| 9 | 0.02785775 | select score from tb_province where name='lily' |
| 10 | 0.00042300 | select score from tb_province where name='lily' limit 1 |
| 11 | 0.00043250 | select score from tb_province where name='lily' limit 1 |
| 12 | 0.00044350 | select score from tb_province where name='lily' limit 1 |
| 13 | 0.00053200 | select score from tb_province where name='lily' limit 1 |
| 14 | 0.00043250 | select score from tb_province where name='lily' limit 1 |
+----------+------------+---------------------------------------------------------+
14 rows in set, 1 warning (0.00 sec)
に示すように、limit 1を採用するとmysql文の効率が大幅に向上します.テーブルが大きくなると、効率が向上します. 私たちはすでに理論と実践のシナリオからlimitの長所を説明しているので、limitが利用できるときにlimitを使うことをお勧めします(もちろん、結果が複数であれば、limit 1は絶対にできませんよ)