簡體   English   中英

執行查詢是從另一個具有外鍵且不存在於其中的表中獲取數據的查詢太慢

[英]It is too slow to excute query that get data from another table which has foreign key and not exists in

我的選擇查詢太慢。

blog_data表

大約2,000,000行。

Field   Type
no      bigint(20)  primary key, auto_increment
title   text
body    text
tags    text
url     varchar(200)
date    datetime

ngram_relation表

Field       Type
no          int     primary key, auto_increment
blogId      bigint(20)
term        varchar(200)
frequency   bigint(20)
TF          float
IDF         float
weight      float
Ns          int(11)

primary key(no),
unique key(blogid, term),
foreign key(blogId) references blog_data(no)

我想獲取不在ngram_relation表中的blog_data.no。 因此,我在下面執行查詢。

select no, title, body, tags, url 
from blog_data where not exists (
    select blogid as gg 
    from ngram_relation 
    group by blogid 
    having blog_data.no=gg
) limit 0, 10000

然后,第一次執行就很好。 第一次執行后, ngram_relation表大約有260,000行。

但是第二次執行不起作用。 剛鎖。

如何修改查詢?

使用左外部聯接可以過濾blog_data中存在的記錄,而不是ngram_relation中存在的記錄。 這應該更快。
像這樣的東西(可能不是確切的代碼):

select top 10000
    from blog_data b
    left outer join ngram_relation n
    on b.no = n.no
    where n.no is null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM