我有两张桌子:
oders (order_id, customer_id, order_date, product_id, order_quantity)
客户 (customer_id, last_name, first_name, favorite_website )
我还有一个应用程序,使用以下命令在表格中经常提问:
SELECT * FROM orders where customer_id = @id
SELECT * FROM orders where customer_id = @id and order_date > @date
SELECT * FROM orders where order_date > @date1 and order_date < @date2
SELECT first_name, last_name, favorite_website FROM customers where last_name = @last_name
SELECT customer_id, first_name, last_name,favorite_website
FROM customers where last_name = @last_name and favorite_website = @site
我想在这些表上找到一些索引,它们的创建将加速上述问题的实现。
我找到的唯一索引是:
1.`create index indexorders on orders (order_id, customer_id, order_date, product_id, order_quantity);`
2.`create index indexcust on customers (customer_id, last_name, first_name, favorite_website );`
是否有比这些更好的其他索引?你能建议吗?
答案 0 :(得分:0)
在订单表上..
在订单表上创建主键(customerid,orderdate
)(如果唯一)或使用以下组合。此索引仅适用于问题中提供的查询组合
create index nci_custid_ordt on dbo.orders
(
customerid,
orderdate)
include
(
order_id, product_id, order_quantity)
对于Customers表
使用以下组合
创建非聚集索引create index nci_lstname_fw on dbo.customers
(
last_name,favorite_website)
include
(customer_id, first_name)
您可以在其各自的表中创建customerid作为主键,orderid作为主键,但如果您不在查询中使用它们,我看不到任何好处..