【问题标题】:Best way to index with these repeated queries使用这些重复查询进行索引的最佳方法
【发布时间】:2017-01-31 19:33:00
【问题描述】:

我正在尝试使用索引,但我还是新手。假设我经常使用以下查询,所以我需要为表 musicmusicians 创建索引。

    SELECT *
    FROM music
    where musician_id = @id

    SELECT *
    FROM music
    where musician_id = @id and date_of_birth > @date

    SELECT *
    FROM music
    where date_of_birth > @date1 and date_of_birth < @date2

    SELECT first_name, last_name, musician_website
    FROM musicians
    where last_name = @last_name

    SELECT customer_id, first_name, last_name,musician_website
    FROM musicians
    where last_name = @last_name and musician_website= @site

如果我想为这些查询创建索引,您认为最好的解决方案是什么。最好为我想要的每一列制作一个(例如 1 个用于musician_id,一个用于date_of_birth 等...)或者可能将它们成对使用:musician_iddate_of_birth,然后@ 987654329@ 和 musician_website 然后只是 date_of_birth 用于第三个查询(我需要它们按日期排序)。 在这里最好的选择是什么?为什么?

【问题讨论】:

  • 至少包括表模式...为什么musicdate_of_birth?我们应该猜到您打算在其中包含一个联接吗?
  • 不,这只是表的一个坏名字。餐桌音乐包含有关音乐家和出生日期的信息。
  • 听起来你需要在索引之前担心normalization
  • 是的,谢天谢地,我正处于项目的开始阶段。感谢您指出这一点。

标签: sql sql-server database indexing


【解决方案1】:

你想要的索引是:

  • music(musician_id, date_of_birth)(查询 1 和 2)
  • music(date_of_birth)(查询 3)
  • musicians(last_name, web_site)(查询 4 ​​和 5)

对于最后一个索引,如果你真的想要,你可以包括customer_idfirst_name

【讨论】:

  • 查询 1 、 2 和 3 引用名为 music 而不是 musicians 的表
  • @SqlZim 。 . .谢谢;眼睛比我好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-30
  • 2012-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多