我查看并尝试了许多不同的解决方案,但似乎没有一个可行。 我正在运行以下查询以从100k表中获取结果:
SELECT * FROM (SELECT expenses.id AS eID, expenses.date,
expenses.receipt, expenses.purpose, expenses.date_submitted,
expenses.expense_type, expenses.description,
expenses.curr_used,expenses.reimbursement_amount,
expenses.hst_amount, expenses.amex_amount, expenses.gl_acct,
expenses.net_amount,expenses.status, users.id AS uID,
users.firstname, users.lastname, users.department,
users.adminship_level, users.is_finance
FROM expenses
INNER JOIN users on expenses.user_id = users.id
) as expense_info
WHERE (
(department="Publishing"
AND adminship_level < 4)
OR uID=8
)
ORDER BY date_submitted DESC
LIMIT 15
支出表是具有10万行的表,而用户表有500个用户。
users表与带user_id外键的费用相关联,并且还包含部门+行政级别的复合索引。
解释收益the following
使用MySQL Workbench运行此程序效果很好,并且速度非常快。但是,将完全相同的查询放入php并使用PDO的prepare()+ execute()函数需要6整秒钟来获取15个结果。
关于如何提高速度的任何想法?我在做什么错了?
我正在使用XAMPP,但没有使用它的MariaDB(相反,我使用的是MySQL 8)
请询问可能有帮助的其他详细信息,我会提供
答案 0 :(得分:0)
Erm,
SELECT e.id eID
, e.date
, e.receipt
, e.purpose
, e.date_submitted
, e.expense_type
, e.description
, e.curr_used
, e.reimbursement_amount
, e.hst_amount
, e.amex_amount
, e.gl_acct
, e.net_amount
, e.status
, u.id uID
, u.firstname
, u.lastname
, u.department
, u.adminship_level
, u.is_finance
FROM expenses e
JOIN users u
ON e.user_id = u.id
WHERE (u.department = "Publishing" AND u.adminship_level < 4)
OR uID=8
ORDER
BY e.date_submitted DESC
LIMIT 15
答案 1 :(得分:0)
这似乎是您需要做的,除去不必要的子查询及其开销
SELECT expenses.id AS eID, expenses.date,
expenses.receipt, expenses.purpose, expenses.date_submitted,
expenses.expense_type, expenses.description,
expenses.curr_used,expenses.reimbursement_amount,
expenses.hst_amount, expenses.amex_amount, expenses.gl_acct,
expenses.net_amount,expenses.status, users.id AS uID,
users.firstname, users.lastname, users.department,
users.adminship_level, users.is_finance
FROM expenses
INNER JOIN users on expenses.user_id = users.id
WHERE uID = :uID
OR (department="Publishing" AND adminship_level < :aship)
ORDER BY date_submitted DESC
LIMIT 15