简体   繁体   English

我需要在复合主键的第二列上建立索引吗?

[英]Do I need an index on the second column of a compound primary key?

My schema is something like below -我的架构如下所示 -

CREATE TABLE `products` (
    `account_id` varchar(100) NOT NULL,
    `product_id` int NOT NULL,
    `product_name` varchar(100) NOT NULL,
    PRIMARY KEY (`account_id`,`product_id`),
    KEY `idx_product_id` (`product_id`)
) 

The app will have several accounts (customers) and each customer will have their own products, like a multi-tenant ecom site.该应用程序将有多个帐户(客户),每个客户都将拥有自己的产品,例如多租户电子商务网站。 A product by itself means nothing and it only makes sense if tied to an account_id .产品本身没有任何意义,只有与account_id相关联才有意义。 Account A (Customer A) can have product 1 and Account B can also have product 1. But the combination of Account and Product should be unique, hence the primary key is on account_id and product_id .账户 A(客户 A)可以有产品 1,账户 B 也可以有产品 1。但是 Account 和 Product 的组合应该是唯一的,因此主键在account_idproduct_id上。

I know that there is no need for an index on account_id , as the primary key will create an index on the account_id / product_id combination.我知道account_id不需要索引,因为主键会在account_id / product_id组合上创建索引。 I am thinking that there is also no separate need for an additional index on product_id , as a product will always be referenced in the context of an account and the primary key index can address this.我认为在product_id上也不需要单独的附加索引,因为产品将始终在帐户的上下文中被引用,并且主键索引可以解决这个问题。

  1. Can I go ahead and remove KEY idx_product_id (product_id) from the table schema above?我可以提前 go 并从上面的表模式中删除KEY idx_product_id (product_id)吗?

  2. In general, do we need an index on the second column of compound primary key?一般来说,我们需要在复合主键的第二列上建立索引吗?

Think of a telephone book.想想电话簿。 It's like an index on (last_name, first_name) .这就像(last_name, first_name)上的索引。 If you look up a person by their last name, it's very helpful that it's sorted by last name first.如果您按姓氏查找一个人,那么首先按姓氏排序非常有帮助。 If you look up a person by both last name and first name, it is also helpful.如果您同时按姓氏和名字查找一个人,这也很有帮助。

But if you use the phone book to try to look up someone only by their first name, the book doesn't help.但是,如果您使用电话簿试图仅通过他们的名字来查找某人,那么这本书将无济于事。 It's not sorted by first name.它不按名字排序。 Is this a problem?这是一个问题吗? Would you ever look up someone by their first name?你会通过他们的名字来查找某人吗? If you need to do that, you would need a second phone book sorted by first name.如果您需要这样做,您将需要按名字排序的第二个电话簿。 But if you never need to do that kind of lookup, you don't need it.但是,如果您从不需要进行这种查找,则不需要它。

It's the same for secondary indexes.二级索引也是如此。 If you always look up your products by account and then by product, then the sort order of the primary key is enough.如果您总是按帐户然后按产品查找产品,那么主键的排序顺序就足够了。 If you sometimes look up products to see which accounts use that product, it would help to have the secondary index with product_id first.如果您有时查找产品以查看哪些帐户使用该产品,那么首先使用product_id的二级索引会有所帮助。

So the answer to your question depends on the queries you intend to run.因此,您的问题的答案取决于您打算运行的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM