用于优化多个表的搜索的索引视图

时间:2012-10-03 15:17:16

标签: sql-server sql-server-2008 search indexed-view

我有一个关于优化复杂搜索查询性能的想法的问题。情况就是这样:

基表,包含要搜索的字段:

Server (uniqueNumber, serverName, hostName)

要加入的表格,以及要搜索的字段:

Customer (Company, vatNumber)
CustomerContact (firstName, lastName, email, telephone, mobile)
Ip (ipAddress)

我的想法是创建一个像这样的索引视图:

viewServers (ServerID, Term)

(ServerID上的聚集索引,术语上非聚集)

这是所有可能数据的并集:

SELECT * FROM (
    SELECT s.ServerID AS ServerID, c.Company AS Term FROM Customer c 
        INNER JOIN Server s ON s.CustomerID = c.CustomerID
    UNION
    SELECT s.ServerID AS ServerID, c.vatNumber AS Term FROM Customer c 
        INNER JOIN Server s ON s.CustomerID = c.CustomerID
    UNION
    SELECT s.ServerID AS ServerID, c.firstName AS Term FROM CustomerContact cc 
        INNER JOIN Customer s ON s.CustomerID = cc.CustomerID
        INNER JOIN Server s ON s.CustomerID = c.CustomerID
    UNION
    SELECT s.ServerID AS ServerID, i.ipAddress AS Term FROM Ip i 
        INNER JOIN Server s ON i.ServerID = i.ServerID
    UNION
) AS result ORDER BY Term

当我想搜索时,我正计划这样做:

SELECT * FROM Server WHERE ServerID IN (SELECT ServerID FROM viewServers WHERE Term LIKE @Term

好主意还是坏主意?

1 个答案:

答案 0 :(得分:0)

如果不了解您的数据库的其他许多内容,很难说这是一个好主意还是坏主意。索引视图可能有助于该特定查询,但可能会损害其他数据库操作。此外,创建非唯一聚簇索引(ServerID)并不是一个好主意。