2011-10-26 20 views
5

LEFT JOINSが5つまたは6つのmysqlクエリがあります。予想どおり、これはかなり遅いです。 〜100の結果しか期待していないことを考慮すると、たくさんの別々のSQLクエリを実行して、手動でそれらを一緒にステッチする方が意味があるかもしれません。私はそれが複数の左の結合で作成された巨大なテーブルのために長い時間がかかると思っています。そうですか?LEFT JOINのパフォーマンスを向上させる

私はこれをRails 3でやっています。アクティブなレコードを作成するのは費用がかかりますが、LEFT JOINSが非常に多くあるよりも速いかもしれないと思います。私はデータベースがどのようにしてフードの下で動作するかについてほとんど知っていません。どんな洞察にも本当に感謝します。


編集:ここでは実際のクエリとテーブルのスキーマがあります

クエリ

SELECT people * people左からは person_organization_relationships .person_id = people .ID ON person_organization_relationshipsperson_organization_relationshipsを登録しよう。 stop_person IS NULL LEFT JOIN person_redirect_relationships AS r_from_others ON r_from_others.parent_id = people .IDとr_from_others.stop_personが NULL残っているが r_to_others.child_id = people .ID ON r_to_others AS person_redirect_relationshipsに参加しr_to_others.stop_personは LEFT NULL IS r_p_check.person_id = r_from_others ON AS r_p_check person_organization_relationshipsを登録しよう。 child_idとr_p_check.stop_person はNULLのままにr_o_check.child_id ON AS r_o_check organization_redirect_relationships を登録しよう= person_organization_relationships .organization_idと r_o_check.stop_organizationは rr_p_check.person_id = r_from_others.child_i ON rr_p_check AS person_organization_relationshipsを登録しようNULL残っていますDおよび rr_p_check.stop_person rr_p_check.organization_id = rr_o_check.child_id ON rr_o_check AS organization_redirect_relationshipsに入会すると、WHERE (((person_organization_relationships .organization_id = 1 OR r_o_check.parent_id = 1)AND r_to_others rr_o_check.stop_organizationがNULL IS NULL残され

create_table "people", :force => true do |t| 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    t.boolean "delta",    :default => true, :null => false 
    end 


    create_table "person_organization_relationships", :force => true do |t| 
    t.integer "person_id" 
    t.integer "organization_id" 
    t.integer "start_person" 
    t.integer "stop_person" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    add_index "person_organization_relationships", ["organization_id"], :name => "index_person_organization_relationships_on_organization_id" 
    add_index "person_organization_relationships", ["person_id"], :name => "index_person_organization_relationships_on_person_id" 
    add_index "person_organization_relationships", ["start_person"], :name => "index_person_organization_relationships_on_start_person" 
    add_index "person_organization_relationships", ["stop_person"], :name => "index_person_organization_relationships_on_stop_person" 

    create_table "person_redirect_relationships", :force => true do |t| 
    t.integer "parent_id" 
    t.integer "child_id" 
    t.integer "start_person" 
    t.integer "stop_person" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    add_index "person_redirect_relationships", ["child_id"], :name => "index_person_redirect_relationships_on_child_id" 
    add_index "person_redirect_relationships", ["parent_id"], :name => "index_person_redirect_relationships_on_parent_id" 
    add_index "person_redirect_relationships", ["start_person"], :name => "index_person_redirect_relationships_on_start_person" 
    add_index "person_redirect_relationships", ["stop_person"], :name => "index_person_redirect_relationships_on_stop_person" 


    create_table "organization_redirect_relationships", :force => true do |t| 
    t.integer "parent_id" 
    t.integer "child_id" 
    t.integer "start_organization" 
    t.integer "stop_organization" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

    add_index "organization_redirect_relationships", ["child_id"], :name => "index_organization_redirect_relationships_on_child_id" 
    add_index "organization_redirect_relationships", ["parent_id"], :name => "index_organization_redirect_relationships_on_parent_id" 
    add_index "organization_redirect_relationships", ["start_organization"], :name => "index_organization_redirect_relationships_on_start_organization" 
    add_index "organization_redirect_relationships", ["stop_organization"], :name => "index_organization_redirect_relationships_on_stop_organization" 
:.parent_idはNULL)OR people

テーブルスキーマを.ID BY(r_p_check.organization_id = 1 OR rr_o_check.parent_id = 1))基であります


このクエリでは結果は得られませんでした。

+ ---- + ------------- + ------------------------ ----------- + -------- + ----------------------------- -------------------------------------------------- --------------------------------------- + ---------- --------------------------------------------- + ---- ----- + -------------------------------------------- ---------------------------- + ------ + -------------- ------------------- + | id | select_type |テーブル|タイプ| possible_keys
|キー| key_len | ref
|行|余分な| + ---- + ------------- + ----------------------------- ------ + -------- + ---------------------------------- -------------------------------------------------- ---------------------------------- + --------------- ---------------------------------------- + --------- + ------------------------------------------------- ----------------------- + ------ + ------------------- -------------- + | 1 |シンプル| person_details |すべて| index_person_details_on_current_p_id
| NULL | NULL | NULL
| 4938 |一時的な使用。 filesortの使用| | 1 |シンプル|人々 | eq_ref | PRIMARY
| PRIMARY | 4 | knolcano_development.person_details.current_p_id
| 1 | | | 1 |シンプル| person_organization_relationships | ref | index_person_organization_relationships_on_person_id、index_person_organization_relationships_on_stop_person | index_person_organization_relationships_on_person_id | 5 | knolcano_development.person_details.current_p_id
| 1 | | | 1 |シンプル| r_from_others | ref | index_person_redirect_relationships_on_parent_id、index_person_redirect_relationships_on_stop_person | index_person_redirect_relationships_on_stop_person | 5 | const
| 3 | | | 1 |シンプル| r_to_others | ref | index_person_redirect_relationships_on_child_id、index_person_redirect_relationships_on_stop_person | index_person_redirect_relationships_on_child_id | 5 | knolcano_development.people.id
| 2 | | | 1 |シンプル| r_p_check | ref | index_person_organization_relationships_on_person_id、index_person_organization_relationships_on_stop_person | index_person_organization_relationships_on_person_id | 5 | knolcano_development.r_from_others.child_id
| 1 | | | 1 |シンプル| r_o_check | ref | index_organization_redirect_relationships_on_child_id、index_organization_redirect_relationships_on_stop_organization | index_organization_redirect_relationships_on_child_id | 5 | knolcano_development.person_organization_relationships.organization_id | 1 | | | 1 |シンプル| rr_p_check | ref | index_person_organization_relationships_on_person_id、index_person_organization_relationships_on_stop_person | index_person_organization_relationships_on_person_id | 5 | knolcano_development.r_from_others.child_id
| 1 | | | 1 |シンプル| rr_o_check | ref | index_organization_redirect_relationships_on_child_id、index_organization_redirect_relationships_on_stop_organization | index_organization_redirect_relationships_on_child_id | 5 | knolcano_development.rr_p_checkorganization_id
| 1 |どこで使う| + ---- + ------------- + ----------------------------- ------ + -------- + ---------------------------------- -------------------------------------------------- ---------------------------------- + --------------- ---------------------------------------- + --------- + ------------------------------------------------- ----------------------- + ------ + ------------------- -------------- + 9行のセット(0.00秒)

しかし、クエリを実行したときには0.14秒かかりました。それは長い時間ですか? memcachedを実装する前に良いクエリがあるかどうかを調べようとしています。

+4

実際のクエリとEXPLAIN出力は、参考になる情報です。 –

+0

私は@JoeStefanelliに同意します。 'EXPLAIN'のクエリ+結果を表示します。 – Tadeck

答えて

3

これにはいくつかの理由が考えられます。クエリのパフォーマンスが悪く、インデックスが不正です。問題のテーブルのためのexplain、query、おそらくcreate tableステートメントは、非常に長い道のりで答えを出すのに役立ちます。

ほとんどの場合、私はこの種のものを見ると、それは貧弱な指標の問題です。

7

多くのJOINは非常に悪い考えかもしれませんが、最初にクエリを表示する必要があります。

まず、クエリを高速化するためにインデックスが必要です。もしあなたが何も持っていなければ、おそらくいくつかを作成するべきです(あなたが実行するクエリに依存します)。

複数のLEFT JOINを実行すると、それらを別々のクエリに分けることができるため、アプリケーションの処理速度が大幅に向上します。

MySQL's documentation on optimization、具体的にはLEFT JOIN optimizationおよびoptimization using indexesを参照できます。これにより、追加の詳細が得られる場合があります。

+0

良い答えです。インデックスの悪い使用が、これらのタイプのシナリオでの悪いパフォーマンスの原因である可能性が高いことを強調したいだけです。 –

関連する問題