- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个来自英国 geonames 网站 ( http://download.geonames.org/export/dump/ ) 的数据库转储。它由大约 60000 条记录组成。
表结构如下:
CREATE TABLE `geoname` (
`geonameid` INT(11) NOT NULL,
`name` VARCHAR(200) NULL DEFAULT NULL,
`asciiname` VARCHAR(200) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`preferredname` VARCHAR(200) NULL DEFAULT NULL,
`alternatenames` VARCHAR(10000) NULL DEFAULT NULL COLLATE `utf8_unicode_ci',
`latitude` DECIMAL(10,7) NULL DEFAULT NULL,
`longitude` DECIMAL(10,7) NULL DEFAULT NULL,
`feature_class` CHAR(1) NULL DEFAULT NULL,
`feature_code` VARCHAR(10) NULL DEFAULT NULL,
`country_code` VARCHAR(2) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`cc2` VARCHAR(60) NULL DEFAULT NULL,
`admin1` VARCHAR(20) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`admin2` VARCHAR(80) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`admin3` VARCHAR(20) NULL DEFAULT NULL,
`admin4` VARCHAR(20) NULL DEFAULT NULL,
`population` INT(11) NULL DEFAULT NULL,
`elevation` INT(11) NULL DEFAULT NULL,
`gtopo30` INT(11) NULL DEFAULT NULL,
`timezone` VARCHAR(40) NULL DEFAULT NULL,
`moddate` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`geonameid`),
INDEX `geoname_name_idx` (`name`),
INDEX `geoname_preferredname_idx` (`preferredname`),
INDEX `geoname_admin1_idx` (`admin1`),
INDEX `geoname_admin2_idx` (`admin2`),
INDEX `geoname_admin3_idx` (`admin3`),
INDEX `geoname_admin4_idx` (`admin4`),
INDEX `geoname_feature_code_idx` (`feature_code`),
INDEX `geoname_feature_class_idx` (`feature_class`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;
我已将索引添加到将在查询中使用的列。该查询用于自动完成功能,但执行时间很长 - 下面的查询花了 26.72 秒,这对于自动完成功能来说非常糟糕:
mysql> SELECT t0.preferredname,
-> t4.preferredname AS town,
-> t3.preferredname AS county,
-> t2.preferredname AS district,
-> t1.preferredname AS admin1,
-> MIN(t0.geonameid)
-> FROM geoname t0
-> LEFT JOIN geoname t1 ON t1.admin1 = t0.admin1 AND t1.feature_code = 'ADM1'
-> LEFT JOIN geoname t2 ON t2.admin2 = t0.admin2 AND t2.feature_code = 'ADM2'
-> LEFT JOIN geoname t3 ON t3.admin3 = t0.admin3 AND t3.feature_code = 'ADM3'
-> LEFT JOIN geoname t4 ON t4.admin4 = t0.admin4 AND t4.feature_code = 'ADM4'
-> WHERE t0.feature_class IN ('P', 'A')
-> AND t0.preferredname LIKE 'preston%'
-> GROUP BY t0.preferredname,
-> t4.preferredname,
-> t3.preferredname,
-> t2.preferredname,
-> t1.preferredname;
+------------------------------+--------------------+--------------------------------+---------------------+----------+-------------------+
| preferredname | town | county | district | admin1 | MIN(t0.geonameid) |
+------------------------------+--------------------+--------------------------------+---------------------+----------+-------------------+
| Preston | NULL | Ellingham | Northumberland | England | 2639911 |
| Preston | NULL | Preston | District of Rutland | England | 2639914 |
| Preston | NULL | Preston | East Yorkshire | England | 2639913 |
| Preston | NULL | Preston District | Lancashire | England | 2639912 |
| Preston | NULL | Weymouth and Portland District | Dorset | England | 2639922 |
| Preston | Dymock | Forest of Dean District | Gloucestershire | England | 2639916 |
| Preston | Preston | Cotswold District | Gloucestershire | England | 2639918 |
| Preston | Preston | Dover District | Kent | England | 2639920 |
| Preston | Preston | North Hertfordshire District | Hertfordshire | England | 2639917 |
| Preston Bagot | Preston Bagot | Stratford-on-Avon District | Warwickshire | England | 2639910 |
| Preston Bisset | Preston Bissett | Aylesbury Vale | Buckinghamshire | England | 2639909 |
| Preston Bissett | Preston Bissett | Aylesbury Vale | Buckinghamshire | England | 7299788 |
| Preston Brook | NULL | Preston Brook | Borough of Halton | England | 7296534 |
| Preston Candover | Preston Candover | Basingstoke and Deane District | Hampshire | England | 2639908 |
| Preston Capes | Preston Capes | Daventry District | Northamptonshire | England | 2639907 |
| Preston District | NULL | Preston District | Lancashire | England | 7290581 |
| Preston Gubbals | NULL | Pimhill | Shropshire | England | 2639906 |
| Preston on Stour | Preston on Stour | Stratford-on-Avon District | Warwickshire | England | 7299630 |
| Preston on the Hill | NULL | Preston Brook | Borough of Halton | England | 2639904 |
| Preston on Wye | NULL | Preston on Wye | Herefordshire | England | 2639903 |
| Preston Park | NULL | NULL | Brighton and Hove | England | 2639921 |
| Preston Patrick | Preston Patrick | South Lakeland District | Cumbria | England | 7298113 |
| Preston Richard | Preston Richard | South Lakeland District | Cumbria | England | 7300167 |
| Preston Road | NULL | Brent | Greater London | England | 2639919 |
| Preston St Mary | Preston St. Mary | Babergh District | Suffolk | England | 2639915 |
| Preston St. Mary | Preston St. Mary | Babergh District | Suffolk | England | 7301329 |
| Preston upon the Weald Moors | NULL | Preston upon the Weald Moors | Telford and Wrekin | England | 2639900 |
| Preston Wynne | NULL | Preston Wynne | Herefordshire | England | 2639899 |
| Preston-on-Tees | NULL | Preston-on-Tees | Stockton-on-Tees | England | 7299560 |
| Preston-under-Scar | Preston-under-Scar | Richmondshire District | North Yorkshire | England | 7291664 |
| Prestonpans | NULL | NULL | East Lothian | Scotland | 2639902 |
+------------------------------+--------------------+--------------------------------+---------------------+----------+-------------------+
31 rows in set (26.72 sec)
mysql>
当我在上述查询中使用探查器时,我得到以下结果:
mysql> select substring_index(event_name,'/',-1) as Status, truncate((timer_end-timer_start)/1000000000000,6) as Duration from performance_schema.events_stages_history_long where event_id>=8215932 and event_id<=9810811;
+----------------------+-----------+
| Status | Duration |
+----------------------+-----------+
| starting | 0.000198 |
| checking permissions | 0.000004 |
| checking permissions | 0.000001 |
| checking permissions | 0.000001 |
| checking permissions | 0.000001 |
| checking permissions | 0.000005 |
| Opening tables | 0.000044 |
| init | 0.000088 |
| System lock | 0.000013 |
| optimizing | 0.000022 |
| statistics | 0.075318 |
| preparing | 0.000059 |
| Creating tmp table | 0.000082 |
| Sorting result | 0.000014 |
| executing | 0.000003 |
| Sending data | 24.472337 |
| Creating sort index | 0.000292 |
| end | 0.000007 |
| query end | 0.000022 |
| removing tmp table | 0.000118 |
| closing tables | 0.000024 |
| freeing items | 0.000278 |
| cleaning up | 0.000001 |
+----------------------+-----------+
23 rows in set (0.00 sec)
当使用 Explain
运行查询时,我得到以下结果:
mysql> EXPLAIN SELECT t0.preferredname,
-> t4.preferredname AS town,
-> t3.preferredname AS county,
-> t2.preferredname AS district,
-> t1.preferredname AS admin1,
-> MIN(t0.geonameid)
-> FROM geoname t0
-> LEFT JOIN geoname t1 ON t1.admin1 = t0.admin1 AND t1.feature_code = 'ADM1'
-> LEFT JOIN geoname t2 ON t2.admin2 = t0.admin2 AND t2.feature_code = 'ADM2'
-> LEFT JOIN geoname t3 ON t3.admin3 = t0.admin3 AND t3.feature_code = 'ADM3'
-> LEFT JOIN geoname t4 ON t4.admin4 = t0.admin4 AND t4.feature_code = 'ADM4'
-> WHERE t0.feature_class IN ('P', 'A')
-> AND t0.preferredname LIKE 'preston%'
-> GROUP BY t0.preferredname,
-> t4.preferredname,
-> t3.preferredname,
-> t2.preferredname,
-> t1.preferredname;
+----+-------------+-------+------------+-------+-----------------------------------------------------+---------------------------+---------+----------------------+------+----------+---------------------------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+-----------------------------------------------------+---------------------------+---------+----------------------+------+----------+---------------------------------------------------------------------+
| 1 | SIMPLE | t0 | NULL | range | geoname_preferredname_idx,geoname_feature_class_idx | geoname_preferredname_idx | 603 | NULL | 55 | 70.01 | Using index condition; Using where; Using temporary; Using filesort |
| 1 | SIMPLE | t1 | NULL | ref | geoname_admin1_idx,geoname_feature_code_idx | geoname_feature_code_idx | 33 | const | 4 | 100.00 | Using where |
| 1 | SIMPLE | t2 | NULL | ref | geoname_admin2_idx,geoname_feature_code_idx | geoname_feature_code_idx | 33 | const | 185 | 100.00 | Using where |
| 1 | SIMPLE | t3 | NULL | ref | geoname_admin3_idx,geoname_feature_code_idx | geoname_admin3_idx | 63 | test.t0.admin3 | 14 | 100.00 | Using where |
| 1 | SIMPLE | t4 | NULL | ref | geoname_admin4_idx,geoname_feature_code_idx | geoname_admin4_idx | 63 | test.t0.admin4 | 7 | 100.00 | Using where |
+----+-------------+-------+------------+-------+-----------------------------------------------------+---------------------------+---------+----------------------+------+----------+---------------------------------------------------------------------+
5 rows in set, 1 warning (0.06 sec)
请注意,我使用的是 group by 子句,因为数据的子级别名称重复。
如何优化该查询?任何建议、提示和技巧将不胜感激。
最佳答案
我猜您希望检索与用户提供的不完整搜索字符串相匹配的地点,然后加入行政管辖区以提供信息更丰富的自动完成功能。
这里的技巧是快速检索候选地点。像这样的子查询就可以解决问题。
SELECT geonameid, preferredname, admin1, admin2, admin3, admin4
FROM geonames
WHERE feature_class IN ('P', 'A')
AND preferredname LIKE 'preston%'
这是查找操作的核心。可以通过
上的复合覆盖索引来加速CREATE INDEX lookup1
ON geonames(feature_class, preferredname, admin1, admin2, admin3, admin4);
尝试这个查询。看看它对你来说是否足够快(亚秒)。如果不是,请尝试此变体:
SELECT geonameid, preferredname, admin1, admin2, admin3, admin4
FROM geonames
WHERE feature_class ='P'
AND preferredname LIKE 'preston%'
UNION ALL
SELECT geonameid, preferredname, admin1, admin2, admin3, admin4
FROM geonames
WHERE feature_class ='A'
AND preferredname LIKE 'preston%'
MySQL 的查询规划器可以随机访问第一个符合条件的行的索引,然后通过顺序扫描索引来检索所需的所有内容。
然后,您可以在 JOIN 操作中使用该子查询的结果集。现在,您只需处理连接中适量的相关行,而不是处理整个困惑的行。
SELECT t0.preferredname,
t4.preferredname AS town,
t3.preferredname AS county,
t2.preferredname AS district,
t1.preferredname AS admin1,
MIN(t0.geonameid) geonameid
FROM (
SELECT geonameid, preferredname, admin1, admin2, admin3, admin4
FROM geonames
WHERE feature_class IN ='P'
AND preferredname LIKE 'preston%'
UNION ALL
SELECT geonameid, preferredname, admin1, admin2, admin3, admin4
FROM geonames
WHERE feature_class ='A'
AND preferredname LIKE 'preston%'
) t0
LEFT JOIN geoname t1 ON t1.admin1 = t0.admin1 AND t1.feature_code = 'ADM1'
LEFT JOIN geoname t2 ON t2.admin2 = t0.admin2 AND t2.feature_code = 'ADM2'
LEFT JOIN geoname t3 ON t3.admin3 = t0.admin3 AND t3.feature_code = 'ADM3'
LEFT JOIN geoname t4 ON t4.admin4 = t0.admin4 AND t4.feature_code = 'ADM4'
GROUP BY t0.preferredname,
t4.preferredname,
t3.preferredname,
t2.preferredname,
t1.preferredname
专业提示:许多单列索引很少会使用多个过滤条件加速查询,尤其是使用 LIKE 'something%'
等范围过滤器。适当的多列索引会更有帮助。
关于mysql - 递归查询性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54316329/
我有三张 table 。表 A 有选项名称(即颜色、尺寸)。表 B 有选项值名称(即蓝色、红色、黑色等)。表C通过将选项名称id和选项名称值id放在一起来建立关系。 我的查询需要显示值和选项的名称,而
在mysql中,如何计算一行中的非空单元格?我只想计算某些列之间的单元格,比如第 3-10 列之间的单元格。不是所有的列...同样,仅在该行中。 最佳答案 如果你想这样做,只能在 sql 中使用名称而
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
我正在为版本7.6进行Elasticsearch查询 我的查询是这样的: { "query": { "bool": { "should": [ {
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
是否可以编写一个查询来检查任一子查询(而不是一个子查询)是否正确? SELECT * FROM employees e WHERE NOT EXISTS (
我找到了很多关于我的问题的答案,但问题没有解决 我有表格,有数据,例如: Data 1 Data 2 Data 3
以下查询返回错误: 查询: SELECT Id, FirstName, LastName, OwnerId, PersonEmail FROM Account WHERE lower(PersonEm
以下查询返回错误: 查询: SELECT Id, FirstName, LastName, OwnerId, PersonEmail FROM Account WHERE lower(PersonEm
我从 EditText 中获取了 String 值。以及提交查询的按钮。 String sql=editQuery.getText().toString();// SELECT * FROM empl
我有一个或多或少有效的查询(关于结果),但处理大约需要 45 秒。这对于在 GUI 中呈现数据来说肯定太长了。 所以我的需求是找到一个更快/更高效的查询(几毫秒左右会很好)我的数据表大约有 3000
这是我第一次使用 Stack Overflow,所以我希望我以正确的方式提出这个问题。 我有 2 个 SQL 查询,我正在尝试比较和识别缺失值,尽管我无法将 NULL 字段添加到第二个查询中以识别缺失
什么是动态 SQL 查询?何时需要使用动态 SQL 查询?我使用的是 SQL Server 2005。 最佳答案 这里有几篇文章: Introduction to Dynamic SQL Dynami
include "mysql.php"; $query= "SELECT ID,name,displayname,established,summary,searchlink,im
我有一个查询要“转换”为 mysql。这是查询: select top 5 * from (select id, firstName, lastName, sum(fileSize) as To
通过我的研究,我发现至少从 EF 4.1 开始,EF 查询上的 .ToString() 方法将返回要运行的 SQL。事实上,这对我来说非常有用,使用 Entity Framework 5 和 6。 但
我在构造查询来执行以下操作时遇到问题: 按activity_type_id过滤联系人,仅显示最近事件具有所需activity_type_id或为NULL(无事件)的联系人 表格结构如下: 一个联系人可
如何让我输入数据库的信息在输入数据 5 分钟后自行更新? 假设我有一张 table : +--+--+-----+ |id|ip|count| +--+--+-----+ |
我正在尝试搜索正好是 4 位数字的 ID,我知道我需要使用 LENGTH() 字符串函数,但找不到如何使用它的示例。我正在尝试以下(和其他变体)但它们不起作用。 SELECT max(car_id)
我有一个在 mysql 上运行良好的 sql 查询(查询 + 连接): select sum(pa.price) from user u , purchase pu , pack pa where (
我是一名优秀的程序员,十分优秀!