简体   繁体   English

MySQL视图-性能不佳

[英]MySQL view - poor performance

I joined a few tables into a view for easier coding. 我将几个表合并到一个视图中以简化编码。 Now when I join a few tables with that view, I get poor performance. 现在,当我使用该视图加入几张表时,性能会变差。 While I'm getting more rows in view, the speed of those queries is dramatically decreased. 在查看更多行的同时,这些查询的速度大大降低了。 I wrote a lot of code using this view so I don't like the solution to rewrite all of those queries :). 我使用此视图编写了很多代码,所以我不喜欢重写所有这些查询的解决方案:)。 Is there any elegant solution to speed up this view when I join it with other tables? 当我将其与其他表一起加入视图时,是否有任何优雅的解决方案可以加快此视图的速度?

This is one of my queries where tickets_parsed is a view: 这是我的查询之一,其中tickets_parsed是一个视图:

SELECT detailValue, SUM(total_tickets) AS total_tickets, SUM(money_in) AS money_in, SUM(handling_charges) AS handling_charges
FROM (
    SELECT bsid, COUNT(*) AS total_tickets, SUM(amount_total) AS money_in, SUM(handling_charges) AS handling_charges
    FROM `bingo`.tickets_parsed
    WHERE tickettime BETWEEN '$date' AND '$date2a'
    AND ticketstatus <> 'CLOSED'
    GROUP BY bsid
    ORDER BY NULL
) AS sub
NATURAL JOIN betshop_details
WHERE detailID = 5
GROUP BY detailValue
ORDER BY NULL

You query uses the view in a sub query, which likely prevents it using indexes. 您在子查询中查询使用该视图,这可能会阻止它使用索引。 But I am not sure that the sub query is necessary. 但是我不确定子查询是否必要。

You could possibly use something like this:- 您可能会使用以下内容:

SELECT a.detailValue, COUNT(*) AS total_tickets, SUM(b.amount_total) AS money_in, SUM(b.handling_charges) AS handling_charges
FROM bingo.tickets_parsed a
INNER JOIN betshop_details b
ON a.bsid = b.bsid
WHERE a.detailID = 5
AND b.tickettime BETWEEN '$date' AND '$date2a'
AND b.ticketstatus <> 'CLOSED'
GROUP BY a.detailValue
ORDER BY NULL

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

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