SQL Server功能运行非常慢

时间:2019-03-13 13:56:56

标签: sql-server

我有一个返回单个日期的函数,它需要10分钟以上的时间才能运行。有什么线索吗? TMNCS拥有超过300万条记录

SQL:     选择dbo.fn_TMNCS_BO_MaxDate

这是我最近的重写 功能:

ALTER FUNCTION [dbo].[fn_TMNCS_MaxDate]
()
RETURNS date
AS
BEGIN
  DECLARE @ResultVar as date
  SELECT @ResultVar= Max([DateOfData]) FROM dbo.TMNCS
  RETURN @ResultVar    
END
GO

谢谢

3 个答案:

答案 0 :(得分:2)

3百万条记录并不是很大,但是它可能足够大,至少可以减慢试图找到最大值的功能。

我可以建议在DateOfData列中添加索引:

CREATE INDEX idx ON dbo.TMNCS (DateOfData);

这应该使SQL Server可以很快找到此列的最大值。假设索引是B树,则SQL Server所要做的就是去树的最左/右边,并在那里读取值。

免责声明:如果您需要表中某个列的最大值(在常规SQL查询的情况下),请考虑使用MAX作为分析函数。

答案 1 :(得分:0)

我会改用window函数:

colors.stream().forEach(cols ->
        System.out.println(cols + " count: " + numbers2
                        .mapToObj(i -> colors[i])
                        .filter(c -> c.equals(cols))
                        .count()
                ));

答案 2 :(得分:0)

如果您可以允许假设表中的最新行具有最大日期,并且您具有聚集索引(尤其是身份) 那么您可以尝试一下:

For i = 2 To ws.Range("E1").CurrentRegion.Rows.Count

If ws.Cells(i, 4).Value Like ("*SSI*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*Settlement instruction*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*delivery Instruction*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*Request form*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.cells(i, 4).Value Like ("*Sales to onboarding*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*Application*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*Doc Check list*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*Prime to Credit*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*Prime to Legal*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*Prime_Legal*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*Prime_Credit*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*LEXIS*") Then ws.Cells(i, 4).EntireRow.Delete
If ws.Cells(i, 4).Value Like ("*Withdrawal Request*") Then ws.Cells(i, 4).EntireRow.Delete

Next i