我的查询速度有问题,显示了我的数据库中的用户列表。 我想显示具有流量信息的用户列表以及与用户一起工作的最后一个员工。
DB看起来像这样:
users表(包含用户信息):
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ip` tinytext NOT NULL,
`name` varchar(64) NOT NULL,
... some other fields
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
KEY `ip` (`ip`(15)) USING BTREE,
)
users_trf表(包含有关用户流量的信息; uid - 来自用户表的用户ID):
CREATE TABLE `users_trf` (
`uid` int(11) unsigned NOT NULL,
`uip` varchar(15) CHARACTER SET latin1 COLLATE latin1_bin NOT NULL,
`in` bigint(20) NOT NULL DEFAULT '0',
`out` bigint(20) NOT NULL DEFAULT '0',
`test` tinyint(4) NOT NULL,
UNIQUE KEY `uid` (`uid`),
KEY `test` (`test`)
)
所有员工名单的员工:
CREATE TABLE `employees` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`full_name` varchar(16) NOT NULL,
PRIMARY KEY (`id`)
)
和log table,其中我存储了员工对客户所做的工作的数据(uid - 来自users表的客户的id,来自employees表的员工的mid-id):
CREATE TABLE `employees_log` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`uid` int(10) unsigned NOT NULL,
`mid` int(10) unsigned NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`note` text NOT NULL,
PRIMARY KEY (`id`)
)
我的查询:
SELECT SQL_CALC_FOUND_ROWS *
FROM users u
LEFT JOIN users_trf t ON u.id = t.uid
LEFT JOIN (
SELECT e2.full_name, e1.uid, e1.mid AS moid
FROM employees_log e1
LEFT JOIN employees e2 ON e1.mid = e2.id
WHERE NOT
EXISTS (
SELECT *
FROM employees_log e3
WHERE e1.uid = e3.uid
AND e1.id < e3.id
)
) e ON e.uid = u.id
LIMIT 0 , 50
它工作得非常慢,我认为这是因为这个子查询(我正在尝试选择最后一位与客户合作的员工):
SELECT e2.full_name, e1.uid, e1.mid AS moid
FROM employees_log e1
LEFT JOIN employees e2 ON e1.mid = e2.id
WHERE NOT
EXISTS (
SELECT *
FROM employees_log e3
WHERE e1.uid = e3.uid
AND e1.id < e3.id
)
是否可以加快查询速度?
UPD:
我添加了索引ALTER TABLE employees_log ADD INDEX ( uid, id );
,查询速度提高了2倍,但我可以让它更快吗?
+----+--------------------+------------+--------+---------------+---------+---------+-------------+-------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+------------+--------+---------------+---------+---------+-------------+-------+--------------------------+
| 1 | PRIMARY | u | ALL | NULL | NULL | NULL | NULL | 12029 | |
| 1 | PRIMARY | t | eq_ref | uid | uid | 4 | bill.u.id | 1 | |
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 2239 | |
| 2 | DERIVED | e1 | ALL | NULL | NULL | NULL | NULL | 2288 | Using where |
| 2 | DERIVED | e2 | eq_ref | PRIMARY | PRIMARY | 4 | bill.e1.mid | 1 | |
| 3 | DEPENDENT SUBQUERY | e3 | ref | PRIMARY,uid | uid | 4 | bill.e1.uid | 1 | Using where; Using index |
+----+--------------------+------------+--------+---------------+---------+---------+-------------+-------+--------------------------+
答案 0 :(得分:1)
首先,我认为你必须向自己说明为什么使用int和bigint。你真的期待这么多数据吗?尝试使用smallint或mediumint,它们需要更少的内存并且速度更快。如果您将mediumint和smallint用作unsigned,它们可以具有相当大的值,请查看:http://dev.mysql.com/doc/refman/5.0/en/integer-types.html
第二,你需要将一些字段组合成一个键:
ALTER TABLE `employees_log ` ADD INDEX ( `uid` , `id` ) ;
答案 1 :(得分:1)
如果要创建新的MySQL表,可以使用INDEX term指定要索引的列。索引是可以在MySQL表上启用以提高性能的额外值
http://www.databasejournal.com/features/mysql/article.php/1382791/Optimizing-MySQL-Queries-and-Indexes.htm
http://www.tutorialspoint.com/mysql/mysql-indexes.htm认为这会给你很多想法..
答案 2 :(得分:0)
您的目标是尝试在日志表中尝试加入该用户的最后一名员工的日志(至少基于密钥),也许只需尝试= <subquery>
而不是NOT EXISTS
?
SELECT e2.full_name, e1.uid, e1.mid AS moid
FROM employees_log e1
LEFT JOIN employees e2 ON e1.mid = e2.id
WHERE e1.id = (
SELECT MAX(e3.id)
FROM employees_log e3
WHERE e1.uid = e3.uid
)
答案 3 :(得分:0)
考虑在employees_log上的列MID和UID上添加索引 - 解释表明此连接未使用索引。
像这样:在employees_log上创建索引化合物(mid,uid)