[英]Anyway to optimize following query?
我在数据库帐户,产品和用户中有三个表。 我写的以下查询执行时间过长。 无论如何,有没有优化以下查询?
"SELECT accounts.id AS aucID, accounts.product_id, accounts.end_time, accounts.price, accounts.win_id,
products.id, products.title, products.ratio, users.id , users.username, users.gender, users.email, users.ip
FROM accounts, products, users
WHERE products.id
IN (
SELECT id
FROM products
WHERE accounts.product_id = id
)
AND users.id
IN (
SELECT id
FROM users
WHERE accounts.win_id = id
)
AND accounts.end_time >= '$monday'
AND accounts.end_time < '$sunday'
GROUP BY accounts.end_time"
尝试使用INNER JOIN
SELECT accounts.id AS aucID,
accounts.product_id,
accounts.end_time,
accounts.price,
accounts.win_id,
products.id,
products.title,
products.ratio,
users.id ,
users.username,
users.gender,
users.email,
users.ip
FROM accounts
INNER JOIN products ON accounts.product_id = products.id
INNER JOIN users ON accounts.win_id = users.id
WHERE accounts.end_time >= '$monday'
AND accounts.end_time < '$sunday'
GROUP BY accounts.end_time
为什么不简化这个-
SELECT
accounts.id AS aucID,
accounts.product_id,
accounts.end_time,
accounts.price,
accounts.win_id,
products.id,
products.title,
products.ratio,
users.id ,
users.username,
users.gender,
users.email,
users.ip
FROM accounts, products, users
WHERE accounts.product_id = products.id
AND accounts.win_id = users.id
AND accounts.end_time >= '$monday'
AND accounts.end_time < '$sunday'
GROUP BY accounts.end_time
您还可以考虑在以下几列中创建索引:
accounts.product_id
accounts.win_id
accounts.end_time
您需要使用显式JOIN而不是隐式。
select
a.id as aucID,
a.product_id ,
a.end_time,
a.price,
a.win_id,
p.id,
p.title,
p.ratio,
u.id as user_id
u.username,
u.gender,
u.email,
u.ip
from accounts a
join products p on p.id = a.product_id
join users u on u.id = a.win_id
WHERE
a.end_time >= '$monday'
and a.end_time < '$sunday'
GROUP BY a.end_time
现在您需要索引,因此首先检查
show indexes from accounts;
show indexes from accounts;
show indexes from users;
检查列是否已被索引(如果未添加)。
alter table accounts add index product_id_idx(product_id);
alter table accounts add index win_id_idx(win_id);
alter table accounts add index end_time_idx(end_time);
alter table products add index pid_idx(id);
alter table users add index uid_idx(id);
请注意,默认情况下,主键已编入索引,因此您可以避免在
alter table products add index pid_idx(id);
alter table users add index uid_idx(id);
如果将它们设置为主键。
此外,请确保JOINING键始终具有相同的数据类型
例如
join products p on p.id = a.product_id
p.id和a.product_id都应具有相同的数据类型ex:int
同样
join users u on u.id = a.win_id
更改后,查看查询性能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.