由于性能问题,无法使用SQL Server数据长度功能获取数据

时间:2019-05-02 18:28:51

标签: c# sql-server performance linq linq-to-sql

我的数据库中有一个文档表。文档表的结构就像

CREATE TABLE [dbo].[Documents]
(
    [DocumentId] [BIGINT] NOT NULL IDENTITY(1, 1),      
    [ObjectType] [VARCHAR](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,       
    [ObjectId] [BIGINT] NOT NULL,       
    [DocumentName] [VARCHAR](250) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,        
    [DocumentFile] [VARBINARY](MAX) NOT NULL,       
    [isTemp] [BIT] NOT NULL CONSTRAINT [DF_Documents_isTemp] DEFAULT ((0)),     
    [IsActive] [BIT] NOT NULL,      
    [RowGuid] [UNIQUEIDENTIFIER] NOT NULL CONSTRAINT [DF_Documents_RowGuid] DEFAULT (newid()),      
    [CreatedBy] [BIGINT] NOT NULL,      
    [CreatedOn] [DATETIME] NOT NULL,        
    [UpdatedBy] [BIGINT] NULL,      
    [UpdatedOn] [DATETIME] NULL             
)       
GO      

-- Constraints and Indexes      
ALTER TABLE [dbo].[Documents] 
    ADD CONSTRAINT [PK_Documents] 
        PRIMARY KEY CLUSTERED ([DocumentId])        
GO      

CREATE NONCLUSTERED INDEX [ix_DocumentName] 
ON [dbo].[Documents] ([DocumentName])       
GO      

CREATE NONCLUSTERED INDEX [ix_ObjectId] 
ON [dbo].[Documents] ([ObjectId])       
GO      

CREATE NONCLUSTERED INDEX [ix_ObjectType] 
ON [dbo].[Documents] ([ObjectType])     
GO

在文档表中有10万条记录。

这是C#方法,它基于objectid来获取单个文档。

public IQueryable<Document> GetDocumentData(long objectId, string objectType)
{
        var searchResults = new MerrickEntities().Documents.Where(c => c.ObjectId == objectId && c.ObjectType == objectType && SqlFunctions.DataLength(c.DocumentFile) > 0);
        return searchResults.AsQueryable();
}

在此查询中,未获取数据,这似乎是由数据长度函数引起的。

如何优化此查询?

0 个答案:

没有答案