2012-02-29 125 views
0

在網站上實現了一項新功能,就好像它是「社交網絡」一樣。優化django查詢

用戶進行的各種操作,接收消息和內容。他們的朋友可以看到所有的動作。

class userMuralEntry(models.Model): 
    user_src = models.ForeignKey(userInfo, related_name="user_src") 
    user = models.ManyToManyField(userInfo, related_name="user_dest") 
    user_follow = models.ManyToManyField(userInfo, related_name="user_follow") 
    private = models.BooleanField(default=False) 
    content = models.TextField() 
    date = models.DateTimeField(default=datetime.now) 
    last_update = models.DateTimeField() 

    object_type = models.ForeignKey(muralType) 

    content_type = models.ForeignKey(ContentType) 
    object_id = models.PositiveIntegerField() 
    content_object = generic.GenericForeignKey('content_type', 'object_id') 

每當我訪問我的首頁,我得到我所有的朋友行動,除了私人的行爲。 所以,我做了這個Django模塊查詢

mural_list = db.userMuralEntry.objects.filter(Q(user__pk__in=friend_list)|Q(user_src__pk__in=friend_list)).order_by('-last_update') 
    m = mural_list.exclude(
    Q(private=True), 
    ~Q(user_src=me_db) & ~Q(user=me_db)) 

    m = m.distinct() 

所有這些代碼生成這個大查詢(這裏解釋輸出)

SELECT DISTINCT myps3t_usermuralentry.id, 
       myps3t_usermuralentry.user_src_id, 
       myps3t_usermuralentry.private, 
       myps3t_usermuralentry.content, 
       myps3t_usermuralentry.DATE, 
       myps3t_usermuralentry.last_update, 
       myps3t_usermuralentry.object_type_id, 
       myps3t_usermuralentry.content_type_id, 
       myps3t_usermuralentry.object_id 
FROM myps3t_usermuralentry 
     LEFT OUTER JOIN myps3t_usermuralentry_user 
     ON (myps3t_usermuralentry.id = 
       myps3t_usermuralentry_user.usermuralentry_id) 
     LEFT OUTER JOIN myps3t_userinfo t4 
     ON (myps3t_usermuralentry.user_src_id = t4.id) 
WHERE ((myps3t_usermuralentry_user.userinfo_id = 20877 
      OR myps3t_usermuralentry.user_src_id = 20877) 
     AND NOT (myps3t_usermuralentry.private = 1 
        AND (NOT ((t4.id = 21095 
           AND NOT (t4.id IS NULL))) 
         AND NOT (myps3t_usermuralentry.id IN 
            (SELECT u1.usermuralentry_id 
            FROM 
            myps3t_usermuralentry_user u1 
                   WHERE ( 
            u1.userinfo_id = 
            21095 
            AND 
            u1.usermuralentry_id 
            IS 
            NOT 
            NULL 
                     ))))) 
     ) 
ORDER BY myps3t_usermuralentry.last_update; 



mysql> explain SELECT DISTINCT `myps3t_usermuralentry`.`id`, `myps3t_usermuralentry`.`user_src_id`, `myps3t_usermuralentry`.`private`, `myps3t_usermuralentry`.`content`, `myps3t_usermuralentry`.`date`, `myps3t_usermuralentry`.`last_update`, `myps3t_usermuralentry`.`object_type_id`, `myps3t_usermuralentry`.`content_type_id`, `myps3t_usermuralentry`.`object_id` FROM `myps3t_usermuralentry` LEFT OUTER JOIN `myps3t_usermuralentry_user` ON (`myps3t_usermuralentry`.`id` = `myps3t_usermuralentry_user`.`usermuralentry_id`) LEFT OUTER JOIN `myps3t_userinfo` T4 ON (`myps3t_usermuralentry`.`user_src_id` = T4.`id`) WHERE ((`myps3t_usermuralentry_user`.`userinfo_id` = 20877 OR `myps3t_usermuralentry`.`user_src_id` = 20877) AND NOT (`myps3t_usermuralentry`.`private` = 1 AND (NOT ((T4.`id` = 21095 AND NOT (T4.`id` IS NULL))) AND NOT (`myps3t_usermuralentry`.`id` IN (SELECT U1.`usermuralentry_id` FROM `myps3t_usermuralentry_user` U1 WHERE (U1.`userinfo_id` = 21095 AND U1.`usermuralentry_id` IS NOT NULL)))))) ORDER BY `myps3t_usermuralentry`.`last_update` DESC LIMIT 20; 
+----+--------------------+----------------------------+-----------------+-------------------------------------------------------------------------------------------+-------------------+---------+---------------------------------------------------+--------+------------------------------------+ 
| id | select_type  | table      | type   | possible_keys                    | key    | key_len | ref            | rows | Extra        | 
+----+--------------------+----------------------------+-----------------+-------------------------------------------------------------------------------------------+-------------------+---------+---------------------------------------------------+--------+------------------------------------+ 
| 1 | PRIMARY   | myps3t_usermuralentry  | ALL    | myps3t_usermuralentry_99bd10ae               | NULL    | NULL | NULL            | 665410 | Using temporary; Using filesort | 
| 1 | PRIMARY   | myps3t_usermuralentry_user | ref    | usermuralentry_id,myps3t_usermuralentry_user_bcd7114e          | usermuralentry_id | 4  | fabriciols_ps3t.myps3t_usermuralentry.id   |  2 | Using where; Using index; Distinct | 
| 1 | PRIMARY   | T4       | eq_ref   | PRIMARY                     | PRIMARY   | 4  | fabriciols_ps3t.myps3t_usermuralentry.user_src_id |  1 | Using where; Using index; Distinct | 
| 2 | DEPENDENT SUBQUERY | U1       | unique_subquery | usermuralentry_id,myps3t_usermuralentry_user_bcd7114e,myps3t_usermuralentry_user_6b192ca7 | usermuralentry_id | 8  | func,const          |  1 | Using index; Using where   | 
+----+--------------------+----------------------------+-----------------+-------------------------------------------------------------------------------------------+-------------------+---------+---------------------------------------------------+--------+------------------------------------+ 
4 rows in set (0.00 sec) 

mysql> show indexes from myps3t_usermuralentry ; 
+-----------------------+------------+--------------------------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+ 
| Table     | Non_unique | Key_name      | Seq_in_index | Column_name  | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | 
+-----------------------+------------+--------------------------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+ 
| myps3t_usermuralentry |   0 | PRIMARY      |   1 | id    | A   |  665410 |  NULL | NULL |  | BTREE  |   | 
| myps3t_usermuralentry |   1 | myps3t_usermuralentry_99bd10ae |   1 | user_src_id  | A   |  22180 |  NULL | NULL |  | BTREE  |   | 
| myps3t_usermuralentry |   1 | myps3t_usermuralentry_ae71a55b |   1 | object_type_id | A   |   8 |  NULL | NULL |  | BTREE  |   | 
| myps3t_usermuralentry |   1 | myps3t_usermuralentry_e4470c6e |   1 | content_type_id | A   |   13 |  NULL | NULL |  | BTREE  |   | 
+-----------------------+------------+--------------------------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+ 
6 rows in set (0.00 sec) 

我做了一些的調整對指數,但其過於緩慢。擁有3-4位朋友的用戶需要5秒鐘的時間!

此表格現在有500,000行。

改進的一些想法?我的Django代碼是否雜亂?最好是在我自己的手中進行查詢?

+0

爲什麼所有,而不是鏈接過濾器/排除通話將q的對象,? – Marcin 2012-02-29 17:08:59

+0

因爲我需要一個OR操作...(用戶或user_src)有沒有辦法做到沒有Q對象? – fabriciols 2012-02-29 17:19:27

+0

你的排除可能會被簡化。 – Marcin 2012-02-29 17:23:17

回答

2

您需要再次閱讀:https://docs.djangoproject.com/en/dev/topics/db/queries/

有很多,你可以簡化代碼的方式。例如:

mural_list = db.userMuralEntry.objects.filter(Q(user__pk__in=friend_list)|Q(user_src__pk__in=friend_list)) 

等同於:

mural_list = db.userMuralEntry.objects.filter(user__pk__in=friend_list)|db.userMuralEntry.objects.filter(user_src__pk__in=friend_list) 

m = mural_list.exclude(
    Q(private=True), 
    ~Q(user_src=me_db) & ~Q(user=me_db)) 

相當於

m = mural_list.exclude(private=True).exclude(user_src=me_db).exclude(user=me_db) 

注意,它可能是更好的地方ORDER_BY條款在最後,避免外部連接在ordered_by查詢。

最終優化的SQL是好心由@fabriciols這裏分享:http://chat.stackoverflow.com/transcript/message/2795524#2795524