Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server SQL Server中聚集索引和非聚集索引的选择_Sql Server_Indexing_Create Table_Clustered Index_Non Clustered Index - Fatal编程技术网

Sql server SQL Server中聚集索引和非聚集索引的选择

Sql server SQL Server中聚集索引和非聚集索引的选择,sql-server,indexing,create-table,clustered-index,non-clustered-index,Sql Server,Indexing,Create Table,Clustered Index,Non Clustered Index,我在一个数据库中有两个独立的表Person和Bank。此外,我在另一个数据库中有一个客户参考表 表:个人-数据库1 PersonId (PK) Name _________________________________ 1 Ram 2 Raj 3 John 4 Emma BankId (PK) Name ____________

我在一个数据库中有两个独立的表Person和Bank。此外,我在另一个数据库中有一个客户参考表

表:个人-数据库1

PersonId (PK)       Name
_________________________________
1                   Ram
2                   Raj
3                   John
4                   Emma
BankId (PK)      Name
_________________________________
1                ICICI
2                HDFC
3                SBI
表:世行-数据库1

PersonId (PK)       Name
_________________________________
1                   Ram
2                   Raj
3                   John
4                   Emma
BankId (PK)      Name
_________________________________
1                ICICI
2                HDFC
3                SBI
表:客户-数据库2

CustomerId (PK)   BankId    PersonId     UserName     PIN
_____________________________________________________________
1                   1         1           person1     7456bb
2                   1         4           person4     NULL
3                   2         1           person1     5691io
4                   3         2           person2     7892yh
5                   3         4           person4     1596pl
我需要高效地执行以下查询,如果没有索引,它将执行全列搜索:

因此,我需要为表客户创建表SQL查询。以下是限制条件

CustomerId int非空主键 BankId int非空-需要索引且非唯一 PersonId int非空-需要索引且非唯一 用户名varchar25非空-需要索引且非唯一 PIN varchar10 NULL-需要索引且非唯一
所述表不具有FK关系,因为所述表位于不同的数据库服务器中。因此,我需要一个高效的表结构来使用JOIN获取记录。我不知道在这种情况下哪个索引是有效的,无论是聚集索引还是非聚集索引。

您已经有了主键。默认情况下,SQL Server中的主键是聚集的,并且一个表上不能有多个聚集索引。如果您多次查询您的表,而不是真正使用当前PK,一个选项是删除它并添加不同的PK,例如,如果bankid和personid的组合在您的表中是唯一的,则可能在bankid和personid上添加一个PK。@ZLK此处如果我想要银行名和人名,我使用以下查询选择c.CustomerId、p.name、b.name、c.UserName,c.PIN FROM DB2.Customers c internal JOIN DB1.Person p ON p.PersonId=c.PersonId internal JOIN DB1.Bank b ON b.BankId=c.BankId在这种情况下,我需要高效的连接。所以,我计划为BankId以及上面列出的其他列添加一个索引。在每一列上添加一个索引是没有效率的,也不会按照您所期望的方式工作。SQL Server在查询表时只选择一个索引,除非强制使用某个特定索引,否则优化人员认为最有效的索引都可以。编辑:现在我想起来了,实际上我不认为您真的需要在表上添加一个这样的查询索引。SQL Optimizer应该已经在所有三个表上很好地使用PKs了。@ZLK-我稍微修改了这个问题。以上三个问题,我经常打电话。