【发布时间】:2015-11-09 03:28:35
【问题描述】:
这是我在论坛上的第一个问题,所以如果我的问题有什么需要改进的地方,请随时告诉我。
我有一个有两个表的大数据库
- “访问”(6M 行),基本上存储网站上的每次访问
- “cityweather”(1M 行)每天存储 3 次许多城市的天气信息
我准确地说,表访问中可能有一些城市不在 cityweather 中,反之亦然,我只需要选择两个表共有的城市。
我首先有一个大查询,我尝试运行但失败了,因此我试图回到加入这两个表的最简单的查询,但性能很糟糕。
SELECT COUNT(DISTINCT(t.city))
FROM visit t
INNER JOIN cityweather d
ON t.city = d.city;
我准确地说,这两个表都在列 city 上建立了索引,并且我已经在两个表上独立地执行了 COUNT(DISTINCT(city)),每个表只需要不到一秒钟的时间。
您可以在下面找到此查询的EXPLAIN 的结果:
您将在表格信息下方找到这两个表格的引擎,尤其是两个表格的引擎:
访问
|姓名 |引擎 |版本 |行格式 |行 |平均行长度 |数据_len |最大数据长度 |索引_len |数据免费 | -------------------------------------------------- -------------------------------------------------- ---------------- |访问 |数据库 | 10 |紧凑 | 6208060 | 85 | 531628032 | 0 | 0 | 0 |SHOW CREATE TABLE output:
CREATE TABLE
`visit` (
`productid` varchar(8) DEFAULT NULL,
`visitdate` datetime DEFAULT NULL,
`minute` int(2) DEFAULT NULL,
`hour` int(2) DEFAULT NULL,
`weekday` int(1) DEFAULT NULL,
`quotation` int(10) unsigned DEFAULT NULL,
`amount` int(10) unsigned DEFAULT NULL,
`city` varchar(100) DEFAULT NULL,
`weathertype` varchar(30) DEFAULT NULL,
`temp` int(11) DEFAULT NULL,
`pressure` int(11) DEFAULT NULL,
`humidity` int(11) DEFAULT NULL,
KEY `Idxvisitdate` (`visitdate`),
KEY `Idxcity` (`city`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
城市天气
|姓名 |引擎 |版本 |行格式 |行 |平均行长度 |数据_len |最大数据长度 |索引_len |数据免费 | -------------------------------------------------- -------------------------------------------------- -------------------------- |城市天气|数据库 | 10 |紧凑 | 1190553 | 73 | 877670784 | 0 | 0 | 30408704 |SHOW CREATE TABLE output:
CREATE TABLE `cityweather` (
`city` varchar(100) DEFAULT NULL,
`lat` decimal(13,9) DEFAULT NULL,
`lon` decimal(13,9) DEFAULT NULL,
`weatherdate` datetime DEFAULT NULL,
`temp` int(11) DEFAULT NULL,
`pressure` int(11) DEFAULT NULL,
`humidity` int(11) DEFAULT NULL,
KEY `Idxweatherdate` (`weatherdate`),
KEY `idx_city` (`city`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
我感觉问题来自type = index 和ref = NULL,但我不知道如何解决它...
You can find here a close question that did not help me solve my problem
谢谢!
【问题讨论】:
-
您要解决的问题是什么?哪里/什么是错误?你能以表格的形式显示想要的结果吗?
-
我的问题是查询持续了几个小时(我允许 60 000 秒)然后崩溃...... MyIsam 或 InnoDb 是什么意思?我对 Mysql 很陌生...
-
如果查询中没有 WHERE 子句,你从哪里得到“使用 where;使用索引”? sqlfiddle.com/#!9/2d35d/3
-
我更新了问题以添加每个表的状态。它们都是 InnoDb。
-
我不知道为什么他们在其中...
标签: mysql join indexing query-performance