mysql
>
select count(*) as cnt,city from city_demo group by city order by cnt desc limit 10;
+-----+---------------+
| cnt | city |
+-----+---------------+
| 8 | Dongying |
| 7 | Omdurman |
| 6 | Etawah |
| 6 | Okara |
| 6 | Tsuyama |
| 6 | Brindisi |
| 6 | Kuwana |
| 6 | Grand Prairie |
| 5 | Fuyu |
| 5 | Siegen |
+-----+---------------+
10 rows in set (0.00 sec)
mysql
>
select count(*) as cnt,left(city,3) as pref from city_demo group by pref order by cnt desc limit 10;
+-----+------+
| cnt | pref |
+-----+------+
| 23 | San |
| 15 | Hal |
| 14 | Cha |
| 14 | al- |
| 12 | Bat |
| 12 | Kor |
| 11 | Don |
| 11 | Shi |
| 10 | La |
| 10 | El |
+-----+------+
10 rows in set (0.00 sec)
mysql
>
select count(*) as cnt,left(city,4) as pref from city_demo group by pref order by cnt desc limit 10;
+-----+------+
| cnt | pref |
+-----+------+
| 14 | San |
| 8 | Dong |
| 7 | Iwak |
| 7 | al-Q |
| 7 | Omdu |
| 6 | Kuwa |
| 6 | Tsuy |
| 6 | Brin |
| 6 | Etaw |
| 6 | Okar |
+-----+------+
10 rows in set (0.00 sec)
可以看到3字节检索到的结果与全文检索相差很大,继续增加到4个字节
mysql
>
select count(*) as cnt,left(city,5) as pref from city_demo group by pref order by cnt desc limit 10;
+-----+-------+
| cnt | pref |
+-----+-------+
| 8 | Dongy |
| 7 | al-Qa |
| 7 | Omdur |
| 6 | Okara |
| 6 | Valle |
| 6 | Grand |
| 6 | Tsuya |
| 6 | Etawa |
| 6 | South |
| 6 | Kuwan |
+-----+-------+
10 rows in set (0.00 sec)
mysql
>
select count(*) as cnt,left(city,6) as pref from city_demo group by pref order by cnt desc limit 10;
+-----+--------+
| cnt | pref |
+-----+--------+
| 8 | Dongyi |
| 7 | Omdurm |
| 6 | Okara |
| 6 | Tsuyam |
| 6 | Valle |
| 6 | Grand |
| 6 | Etawah |
| 6 | Brindi |
| 6 | Kuwana |
| 5 | Haldia |
+-----+--------+
10 rows in set (0.01 sec)
mysql
>
select count(distinct city)/count(*) from city_demo;
+-------------------------------+
| count(distinct city)/count(*) |
+-------------------------------+
| 0.4333 |
+-------------------------------+
1 row in set (0.00 sec)
mysql
>
select count(distinct left(city,3))/count(*) as sel3,count(distinct left(city,4))/count(*) as sel4,count(distinct left(city,5))/count(*) as sel5, count(distinct left(city,6))/count(*) as sel6 from city_demo;
+--------+--------+--------+--------+
| sel3 | sel4 | sel5 | sel6 |
+--------+--------+--------+--------+
| 0.3408 | 0.4100 | 0.4225 | 0.4300 |
+--------+--------+--------+--------+
1 row in set (0.00 sec)
mysql
>
explain select * from city_demo where city like 'Jin%' \G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: city_demo
partitions: NULL
type: range
possible_keys: city
key: city
key_len: 8
ref: NULL
rows: 4
filtered: 100.00
Extra: Using where
1 row in set, 1 warning (0.00 sec)