此查詢針對單個表格。該表有60個屬性叫做「ATTRIBUTE1」到「attribute60」,而且都是類型:如何加快這個針對MyISAM表的巨大查詢?
varchar(255)
還有一種被稱爲「brand_id」列,它是:
int(11)
以下關鍵存在於表中:
brand_id, attribute1, attribute2, attribute3
查詢是如下:
SELECT distinct attribute1
from brands b inner join product_applications pa on pa.brand_id = b.id
where b.id in (1,372,373,374,375,376,378,381,452,
453,454,455,456,457,458,461,474,476,
544,480,563,508,512,513,516,517,519,520,521,
522,524,525,527,528,529,533,538,539,540,542,
546,547,548,555,556,557,642,643,644,645,646,
647,648,649,650,651,652,653,654,655)
需要10秒以上。該表有5,735,673行。
此查詢需要不超過2秒。我無法弄清楚如何編寫它,或者如果我需要某種備用表結構,可以這樣做。任何人都可以提供建議嗎?
我曾經在WHERE子句中使用IN,但是在另一個論壇中建議這種醜陋的方式由於連接而更快。我真的不知道這意味着什麼,但速度更快,但仍然非常慢。
+----+-------------+-------+-------+----------------------------------------+--------------------+---------+-----------+-------+-------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+-------+----------------------------------------+--------------------+---------+-----------+-------+-------------------------------------------+ | 1 | SIMPLE | b | range | PRIMARY | PRIMARY | 4 | NULL | 60 | Using where; Using index; Using temporary | | 1 | SIMPLE | pa | ref | brand_search_index,parttype_search_idx | brand_search_index | 5 | mcp5.b.id | 57356 | Using where; Using index | +----+-------------+-------+-------+----------------------------------------+--------------------+---------+-----------+-------+-------------------------------------------+ 2 rows in set (0.04 sec)
啊...有沒有辦法可以改變這種1.使用InnoDB和2.有一個健全的模式? – cwallenpoole 2011-06-14 18:07:43
如果您將DISTINCT離開查詢,查詢將返回表PA中500萬行中的多少行? – Tim 2011-06-14 18:19:39
InnoDB將如何加快查詢速度?爲什麼這張桌子不理智?所有這些屬性之間的差異是這樣的,它會導致很多行,但不是那麼統一以至於可以關聯地分解。 – AKWF 2011-06-14 18:42:55