2016-07-14 2 views
-2

나는 런타임을 줄이기 위해 3 가지 방법을 시도했지만 아무리 나에게 해결책을 제안 할 수 있다면 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 

, , 하위 쿼리를 시도했지만 실패했습니다. 누구나 솔루션을 남겨주세요.

답변

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 변수와 밀접한 관련이 있습니다.이 값을 늘리면 max_connections를 늘리면 허용되는 연결 수가 늘어나는 것처럼 MySQL이 많은 수의 테이블을 열어 둘 수 있습니다. 여러 데이터베이스 및 테이블에서 쿼리를 수신하는 대용량 서버가있는 경우이 값을 변경하십시오.

+0

[가장 일반적인 SQL 최적화는 무엇입니까?] (http://stackoverflow.com/questions/1332778/what-are-your-most-common-sql-optimizations) – FirstOne

+0

어떻게 할 수 있습니까? 따라서 메모리 버퍼의 크기 " –

+0

필요한 필드를 반환하고 필요한 행만 반환하여 반환되는 데이터 양을 줄입니다. 이는 데이터를 반환하는 모든 쿼리에 대해 수행 할 때 가장 일반적입니다. –

관련 문제