在我告訴查詢這裏有相關的表定義:SQL查詢慢於預期
CREATE TABLE phpbb_posts (
topic_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL,
poster_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL,
KEY topic_id (topic_id),
KEY poster_id (poster_id),
);
CREATE TABLE phpbb_topics (
topic_id mediumint(8) UNSIGNED NOT NULL auto_increment
);
這裏是我想要做的查詢:
SELECT p.topic_id, p.poster_id
FROM phpbb_topics AS t
LEFT JOIN phpbb_posts AS p
ON p.topic_id = t.topic_id
AND p.poster_id <> ...
WHERE p.poster_id IS NULL;
基本上,查詢是一個嘗試查找除目標用戶以外的其他人發佈的次數爲零的所有主題。換句話說,發佈的唯一人員是目標用戶。
問題是查詢需要超長時間。這裏的解釋是:
Array
(
[id] => 1
[select_type] => SIMPLE
[table] => t
[type] => index
[possible_keys] =>
[key] => topic_approved
[key_len] => 1
[ref] =>
[rows] => 146484
[Extra] => Using index
)
Array
(
[id] => 1
[select_type] => SIMPLE
[table] => p
[type] => ref
[possible_keys] => topic_id,poster_id,tid_post_time
[key] => tid_post_time
[key_len] => 3
[ref] => db_name.t.topic_id
[rows] => 1
[Extra] => Using where; Not exists
)
我一般的假設,當涉及到SQL爲連接的任何是超級快,可以在任何時間完成所有假設所有相關列是主鍵和外鍵(在這是他們的情況)。
我嘗試了一些其他的疑問:
SELECT COUNT(1)
FROM phpbb_topics AS t
JOIN phpbb_posts AS p
ON p.topic_id = t.topic_id;
這很快返回353340。
然後我做這些:
SELECT COUNT(1)
FROM phpbb_topics AS t
JOIN phpbb_posts AS p
ON p.topic_id = t.topic_id
AND p.poster_id <> 77198;
SELECT COUNT(1)
FROM phpbb_topics AS t
JOIN phpbb_posts AS p
ON p.topic_id = t.topic_id
WHERE p.poster_id <> 77198;
而且兩者那些需要相當一段時間(15-30秒之間)。如果我將<>更改爲a =,則根本不需要任何時間。
我在做一些不正確的假設嗎?也許我的數據庫只是foobar'd?
您應該指定您正在使用的數據庫和版本,方法是將其作爲問題中的標記。 –
根據表格的內容,您會期望連接中的不平等條件花費比平等更長的時間。也許如果你在桌子上放一些指標,你會看到一個改進。 – Xophmeister