如何减少此查询运行时?

时间:2016-07-14 18:21:27

标签: php mysql sql

我尝试了三种方法来减少它的运行时间,但我不能,仍需要97.00456秒,如果有人可以请我建议解决方案。

SELECT r.id, r.first_name, r.phone, r.checkin_id, r.device_id, replace(c.`name`,' ','') as device, r.model_id,
    cv.`name` as model, r.color_id, cvc.`name` as color, r.network_id, cn.`name` as network, r.problem_id, cp.`name` as problem,
    #get pid by concat device detail
    IFNULL(
        (SELECT id 
        FROM product 
        WHERE 
            #if not admin
            #store_id=$input_by AND 
            #if not admin 
            name=concat(replace(c.`name`,' ',''),', ',cv.`name`,' - ',cp.`name`) 
        ORDER BY id DESC 
        LIMIT 1)
        ,
        (SELECT id 
        FROM product 
        WHERE 
            #if not admin
            #store_id=$inout_by AND 
            #if not admin 
            name=concat(replace(c.`name`,' ',''),' , ',cv.`name`,' - ',cp.`name`) 
        ORDER BY id DESC 
        LIMIT 1)
    ) AS pid,
    #get pid by concat device detail END
    IFNULL(
        (SELECT id 
        FROM coustomer as cus 
        WHERE cus.firstname=r.first_name AND cus.phone=r.phone 
        LIMIT 1)
        ,
        0
    ) as cid,
    pc.invoice_id as invoice_id,
    #(SELECT count(id) FROM invoice WHERE invoice_id=IFNULL((SELECT invoice_id FROM pos_checkin WHERE checkin_id=r.checkin_id),0) LIMIT 1) AS invoice_status,
    SUM(ip.amount) as paid,
    r.date,
    r.input_by,
    r.status 
FROM checkin_request as r
LEFT JOIN pos_checkin as pc on pc.checkin_id=r.checkin_id
LEFT JOIN invoice_payment as ip on ip.invoice_id=pc.invoice_id
LEFT JOIN checkin as c ON c.id=r.device_id
LEFT JOIN checkin_version as cv ON cv.id=r.model_id
LEFT JOIN checkin_version_color as cvc ON cvc.id=r.color_id
LEFT JOIN checkin_network as cn ON cn.id=r.network_id
LEFT JOIN checkin_problem as cp ON cp.id=r.problem_id 
WHERE r.checkin_id IN (
    SELECT crt.checkin_id 
    FROM checkin_request_ticket as crt
    )
GROUP BY r.checkin_id 
ORDER BY id DESC
LIMIT 5 

我尝试使用左连接, 尝试使用嵌套查询, 尝试使用子查询但失败了。 如果有人这样做,请留下解决方案。

1 个答案:

答案 0 :(得分:2)

#1:使用索引

MySQL允许您索引数据库表,从而可以在不先执行全表扫描的情况下快速查找记录,从而显着加快查询执行速度。每个表最多可以有16个索引,MySQL也支持多列索引和全文搜索索引。

#2:优化查询效果

在分析查询性能时,考虑EXPLAIN关键字也很有用。当放置在SELECT查询前面时,此关键字描述了MySQL打算如何执行查询以及成功传递结果集所需处理的行数。

更改索引缓冲区大小(key_buffer)

This variable controls the size of the buffer used when handling table indices (both read and write operations). The MySQL manual recommends that this variable be increased "to as much as you can afford" to ensure you get the best performance on indexed tables, and recommends a value equivalent to about 25 percent of the total system memory. This is one of the more important MySQL configuration variables and if you're interested in optimizing and improving performance, trying different values for the key_buffer_size variable is a good place to start.
Altering Table Buffer Size (read_buffer_size)
When a query requires a table to be scanned sequentially, MySQL allocates a memory buffer to this query. The read_buffer_size variable controls the size of this buffer. If you find that sequential scans are proceeding slowly, you can improve performance by increasing this value, and hence the size of the memory buffer.

设置最大打开表数(table_cache) 此变量控制MySQL可以在任何时间打开的最大表数,从而控制服务器响应传入请求的能力。此变量与max_connections变量密切相关 - 增加此值允许MySQL保持更多数量的表打开,就像增加max_connections增加允许的连接数一样。如果您有一个在多个不同数据库和表上接收查询的高容量服务器,请考虑更改此值。