简体   繁体   English

MYSQL 查看查询性能问题

[英]MYSQL View Query Performance Issue

I have 5 SQL tables我有 5 张 SQL 表

  • store店铺
  • staff职员
  • departments部门
  • sold_items已售商品
  • staff_rating员工评级

I created a view that JOINs this four of the tables together.我创建了一个将这四个表连接在一起的视图。 The last table (staff_rating),I want to get the rating column at a time close to when items was sold (sold_items.date) for the view rows.最后一张表(staff_rating),我想在接近视图行的商品售出时间(sold_items.date)的时间获取评级列。 I have tried the following SQL Queries which works but have performance issues.我尝试了以下 SQL 查询,这些查询有效但存在性能问题。

SQL QUERY 1 SQL 查询 1

SELECT s.name,
       s.country,
       d.name,
       si.item, 
       si.date, 
       (SELECT rating 
        FROM staff_ratings 
        WHERE staff_id = s.id 
        ORDER BY DATEDIFF(date, si.date) LIMIT 1) AS rating, 
       st.name, 
       st.owner
   FROM store st
   LEFT OUTER JOIN staff s ON s.store_id = st.id
   LFET JOIN departments d ON d.store_id = st.id
   LEFT JOIN sold_items si ON si.store_id = st.id

SQL QUERY 2 SQL 查询 2

SELECT s.name,
       s.country,
       d.name,
       si.item, 
       si.date, 
       si.rating , 
       st.name, 
       st.owner
FROM store st
LEFT OUTER JOIN staff s ON s.store_id = st.id
LFET JOIN departments d ON d.store_id = st.id
LEFT JOIN (SELECT *,
                  (SELECT rating 
                   FROM staff_ratings 
                   WHERE staff_id = si.staff_id 
                   ORDER BY DATEDIFF(date, si.date) LIMIT 1) AS rating 
           FROM sold_items) si ON si.store_id = st.id

SQL Query 2 is faster than SQL Query 1. But Both still have performance issue. SQL 查询 2 比 SQL 查询 1 快。但两者仍然存在性能问题。 Appreciate help for a query with better performance.感谢对性能更好的查询的帮助。 Thanks in advance.提前致谢。

Your query doesn't look right to me (as mentioned in a comment on the original post; lacking staff_id in the join on the sales, etc)您的查询对我来说看起来不正确(如对原始帖子的评论中所述;在销售的联接中缺少 staff_id 等)

Ignoring that, one of your biggest performance hits is likely to be this...忽略这一点,你最大的表现之一可能是这个......

ORDER BY DATEDIFF(date, si.date) LIMIT 1

That order by can only be answered by comparing EVERY record for that staff member to the current sales record.该订单只能通过将该员工的每条记录与当前销售记录进行比较来回答。

What you ideally want to be able to do is find the appropriate staff rating from an index, and not to have to run computations that involve dates from both the ratings table and the sales table.理想情况下,您希望能够从索引中找到适当的员工评级,而不必运行涉及评级表和销售表中日期的计算。

If, for example, you wanted "the most recent rating BEFORE the sale", the query can be substantially improved...例如,如果您想要“销售前的最新评级”,则可以大大改进查询......

SELECT
   s.name,
   s.country,
   d.name,
   si.item, 
   si.date, 
   (
     SELECT sr.rating 
       FROM staff_ratings sr
      WHERE sr.staff_id = s.id
        AND sr.date <= si.date
   ORDER BY sr.date DESC
      LIMIT 1
   )
    AS rating, 
   st.name, 
   st.owner
FROM store st
LEFT JOIN staff s ON s.store_id = st.id
LFET JOIN departments d ON d.store_id = st.id
LEFT JOIN sold_items si ON si.store_id = st.id

Then, with an index for staff_ratings(staff_id, date, rating) the optimiser can very quickly look up which rating to use, without having to scan Every Single Rating for that staff member.然后,使用staff_ratings(staff_id, date, rating)的索引,优化器可以非常快速地查找要使用的评级,而无需扫描该员工的每个单一评级。

Why DATEDIFF ?为什么是DATEDIFF Would something like this work better?像这样的东西会更好吗? If so, the given index will make it work much faster.如果是这样,给定的索引将使其工作得更快。

WHERE staff_id = s.id
  AND s.date >= s1.date
ORDER BY s.date
LIMIT 1

And INDEX(staff_id, date)INDEX(staff_id, date)

Do you need LEFT JOIN ?你需要LEFT JOIN吗? Perhaps plain JOIN ?也许是普通JOIN

d may benefit from INDEX(store_id, name) d可能受益于INDEX(store_id, name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM