2016-07-14 13 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がクエリを実行する方法と、結果セットを正常に配信するために処理する必要がある行数が記述されます。

ので、この変数は、テーブルのMySQLの最大数が一度にオープンできる制御最大オープンテーブルの数(table_cacheのを)設定インデックスバッファサイズ(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. 

を変更し、着信要求に応答するサーバーの機能を制御します。この変数は、max_connections変数と密接に関連しています。この値を大きくすると、max_connectionsを大きくすると接続数が増えるため、MySQLはより多くのテーブルを開くことができます。複数の異なるデータベースとテーブルでクエリを受信する大容量サーバーを使用している場合は、この値を変更することを検討してください。

+0

[最も一般的なSQLの最適化は何ですか?](http://stackoverflow.com/questions/1332778/what-are-your-most-common-sql-optimizations) – FirstOne

+0

どうすればよいのですか?したがってメモリバッファのサイズ " –

+0

必要なフィールドだけを返し、必要な行だけを返すことによって返されるデータの量を減らします。データを返すすべてのクエリに対して、これが最も一般的です。 –

関連する問題