2011-03-17 69 views
2

我有mysql-表稱爲產品具有以下結構/內容:MySQL的優化

id (int), public_id (int), deleted (bool), online (bool) 
1   1    0    1 
2   1    0    0 
3   1    1    0 
4   2    0    1 
5   2    0    1 

我的問題是,如何選擇全部,當前在線,不刪除產品。在這個例子中只記錄5(public_id 2)。相同的public_id表示相同的產品(分組)。id越高,信息越新(排序)。並且產品需要而不是已刪除(其中)。還有一些其他地方的聲明,在這種情況下與在線領域。

我需要所有方面(分組,排序和地方),但我不知道如何。

有什麼建議嗎?的

結果從Galz解釋查詢:

id select_type   table   type possible_keys key  key_len ref rows Extra 
1 PRIMARY    nomis_houses ref online   online 1  const 8086 Using where 
2 DEPENDENT SUBQUERY nomis_houses index NULL   house_id 4  NULL 9570 Using filesort 

PS。此查詢的伎倆,但非常,非常,非常緩慢:

select * from 
(select * from 
    (select * from products order by id desc) tmp_products_1 
group by public_id) tmp_products_2 
where deleted = '0' AND online = '1' 

回答

2

基於薩欽的答案,你的評論,也許這可以幫助:

select * from products where id in 
(
    select max(id) as id from products 
    where sum(deleted) = 0 
    group by public_id 
) 
and online = 1 

編輯由Pentium10

查詢可以改寫成

SELECT * 
FROM products p 
     JOIN (SELECT MAX(id) AS id 
      FROM products 
      HAVING SUM(deleted) = 0 
      GROUP BY public_id) d 
     ON d.id = p.id 
WHERE online = 1 

你需要的索引:

  • (ID,在線)
  • (public_id,刪除,ID)
+0

內部查詢中不允許使用「max(id)」。內部查詢應該只返回一列。 – Tim 2011-03-17 17:36:38

+0

@Tim - 已更正,現在嘗試 – Galz 2011-03-17 17:49:03

+0

@Galz - 結果正確,但速度太慢,無法使用。 MySQL已經忙了幾分鐘。 – Tim 2011-03-17 17:59:19

0
select * from products where public_id in 
(
    select public_id from products  
    group by public_id 
    having sum(deleted) = 0 
) 
and online = 1 

嘗試,如果能正常工作。子查詢會提供所有未被刪除的public_id,然後運行其他聯機的篩選器。

+2

應該是'由具有public_id總和組(刪除)=的0'代替其中+組由 – 2011-03-17 17:21:44

+0

這兩種解決方案是不夠的。我編輯了這個例子,我添加了另一個記錄,同樣用public_id 2。應該選擇第五行(最新版本的public_id 2)而不是第四行。 – Tim 2011-03-17 17:28:49

+0

@Simen - 謝謝。根據您的評論編輯我的答案。 – 2011-03-17 18:13:05

0

嗯,這工作,但我不知道它的效率。基於薩欽的回答

select p.* from products p where p.public_id in 
(
    select p2.public_id from products p2 
    group by p2.public_id 
    having sum(p2.deleted) = 0 
) 
and p.online = 1 
and p.id = (select max(p3.id) from products p3 where p3.public_id = p.public_id); 
+0

確實有效,但它速度很慢:-)我在一分鐘前開始了這個查詢,並且在我們發言時它仍在運行。沒有辦法消除WHERE子句中的查詢,因爲這些顯然是瓶頸?也許我應該考慮對錶結構採用不同的方法? 3分鐘,並計數。 – Tim 2011-03-17 17:45:30