2013-12-23 42 views
0

誰能告訴我,如果有以下的SQLite聲明..SQLITE多列索引與單列指標

CREATE TABLE products (
    category_id INT NOT NULL, /* foreign key on `categories` */ 
    product_id TEXT NOT NULL, /* must be string, unique only within a category */ 
    info  TEXT, 
    PRIMARY KEY (category_id, product_id) /* our PK */ 
) WITHOUT ROWID 

...如果我還是應該建立以下指標

CREATE INDEX products_by_category_id 
    ON products(category_id) 
CREATE INDEX products_by_product_id 
    ON products(product_id) /* not sure I need this one at all */ 

,或者這是多餘的某種程度上由於多列PK INDEX定義在CREATE TABLE

回答

0

是的,這是多餘的。

索引列可以從左到右使用。例如。 PK索引可以與category_id單獨使用或者與category_idproduct_id一起使用。因此,僅當category_id上的索引是無用的,而product_id上的索引可能非常有用,前提是您的查詢可以在product_id上進行過濾,但不會在category_id上進行過濾。