2011-06-14 53 views
0

此查詢針對單個表格。該表有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) 
+0

啊...有沒有辦法可以改變這種1.使用InnoDB和2.有一個健全的模式? – cwallenpoole 2011-06-14 18:07:43

+0

如果您將DISTINCT離開查詢,查詢將返回表PA中500萬行中的多少行? – Tim 2011-06-14 18:19:39

+1

InnoDB將如何加快查詢速度?爲什麼這張桌子不理智?所有這些屬性之間的差異是這樣的,它會導致很多行,但不是那麼統一以至於可以關聯地分解。 – AKWF 2011-06-14 18:42:55

回答

1

這似乎是一個奇怪的查詢,所以我可能失去了一些東西,但我認爲你可以用IN子句做到這一點:

SELECT distinct attribute1 
from product_applications pa WHERE brand_id IN (1, 372, ..., 655) 

我認爲這是更快 - 它肯定清潔劑 - 但如果沒有你可能需要添加有關索引一個小細節,也許一個EXPLAIN查詢等

+2

_maybe_ EXPLAIN查詢?永遠不要離開家! :-) – Wiseguy 2011-06-14 18:16:07

+0

這就是我原來的樣子,而我上面這樣瘋狂的做法更快。我用EXPLAIN的結果更新了我的問題。 – AKWF 2011-06-14 18:41:19

1

你的表是這樣的:

 brand...attr1......attr2..........attr3 
     BRAND...CATEGORY...PRODTYPE....PRODUCT 

例如

 DOVE....HOME.......SOAP.......dish washing liquid 

而且您想要爲指定的品牌列表獲取不同的產品類別集。但是,您擁有超過500萬種產品,因此無法使用單個表格立即完成。這是通常的處理方式是有多個相關的表:

 BRANDS(brandid, brandname) 
     CATEGORIES (categoryid, categoryname) 
     PRODTYPES (prodtypeid, prodtypename) 
     BRANDCATEGORIES (brandid, categoryid) EDIT: put a unique constraint on (brandid, categoryid) 
     PRODUCTS(brandid, categoryid, prodtypeid, productname) 

然後,您的查詢會是這樣:

 select distinct categories.categoryid, categoryname 
     from brands inner join categories 
     on brand.brandid = brandcategories.brandid 
     and categories.categoryid = brandcategories.categoryid 
     where brands.brandid in (....)