Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.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 如何使用多个连接和冗余标准优化查询?_Sql_Sql Server_Sql Server 2008_Tsql - Fatal编程技术网

Sql 如何使用多个连接和冗余标准优化查询?

Sql 如何使用多个连接和冗余标准优化查询?,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,我真的希望你能帮我做这件事。这是我目前的代码,到目前为止,这是我能编写的最好的代码 SELECT max(t2.pmskey) as pmskey, max(cast(t1.recdate as datetime)) as recdate, max(t2.mrtype) as mrtype, max(cast(t1.mrdate as datetime)) as mrdate, max(t2.CaseNo) as CaseNo, max(t2.pmclin)

我真的希望你能帮我做这件事。这是我目前的代码,到目前为止,这是我能编写的最好的代码

 SELECT
      max(t2.pmskey) as pmskey, max(cast(t1.recdate as datetime)) as recdate,
      max(t2.mrtype) as mrtype, max(cast(t1.mrdate as datetime)) as mrdate,
      max(t2.CaseNo) as CaseNo, max(t2.pmclin) as pmclin,
      max(cast(t1.nexteval as datetime)) as nexteval,
      max(cast(t1.repdate as datetime)) as repdate,
      max(t3.mrprocedure) as mrprocedure, max(t2.med_stat) as med_stat,
      max(cast(t1.med_stateff as datetime)) as med_stateff,
      max(t2.clincontact) as clincontact,
      max(cast(t1.datemodf as datetime)) as datemodf, max(t2.modfby) as modfby,
      max(cast(t1.inceptiondate as datetime)) as inceptiondate,
      max(t2.createdby) as createdby, max(cast(t1.date_ent as datetime)) as date_ent,
      max(t2.ppihandler) as ppihandler

 FROM
      tblpms as t1

 JOIN
      (
      select * from tblpms where lower(CaseNo) like '%tr13-011%'

 AND
      cast(mrdate as datetime) IN (select max(cast(mrdate as datetime))
      from tblpms where lower(CaseNo) like '%tr13-011%')
      ) as t2

      on t1.CaseNo COLLATE DATABASE_DEFAULT = t2.CaseNo COLLATE DATABASE_DEFAULT

 JOIN
      (
      select * from tblpms where lower(CaseNo) like '%tr13-011%'

 AND
      lower(mrprocedure) is not null and cast(nexteval as datetime)
      in (select max(cast(nexteval as datetime)) from
      tblpms where lower(CaseNo) like '%tr13-011%')
      ) as t3

      on t1.CaseNo COLLATE DATABASE_DEFAULT = t2.CaseNo COLLATE DATABASE_DEFAULT

      and lower(t2.CaseNo) like '%tr13-011%'
lowerCaseNo的标准在所有连接中都被复制,我不知道如何减少这些。我相信有些事情是可以做的。如果还有其他可以优化的东西,请在回答中包括它

这是我目前根据以下答案提出的问题

 SELECT
      max(t2.pmskey) as pmskey, max(cast(t1.recdate as datetime)) as recdate,
      max(t2.mrtype) as mrtype, max(cast(t1.mrdate as datetime)) as mrdate,
      max(t2.CaseNo) as CaseNo, max(t2.pmclin) as pmclin,
      max(cast(t1.nexteval as datetime)) as nexteval,
      max(cast(t1.repdate as datetime)) as repdate,
      max(t3.mrprocedure) as mrprocedure, max(t2.med_stat) as med_stat,
      max(cast(t1.med_stateff as datetime)) as med_stateff,
      max(t2.clincontact) as clincontact,
      max(cast(t1.datemodf as datetime)) as datemodf, max(t2.modfby) as modfby,
      max(cast(t1.inceptiondate as datetime)) as inceptiondate,
      max(t2.createdby) as createdby, max(cast(t1.date_ent as datetime)) as date_ent,
      max(t2.ppihandler) as ppihandler

 FROM
      tblpms as t1

 JOIN
      (
      select * from tblpms where CaseNo = 'TR13-011-CRW'

 AND
      cast(mrdate as datetime) IN (select max(cast(mrdate as datetime))
      from tblpms where CaseNo = 'TR13-011-CRW')
      ) as t2

      on t1.CaseNo COLLATE DATABASE_DEFAULT = t2.CaseNo COLLATE DATABASE_DEFAULT

 JOIN
      (
      select * from tblpms where CaseNo = 'TR13-011-CRW'

 AND
      lower(mrprocedure) is not null and cast(nexteval as datetime)
      in (select max(cast(nexteval as datetime)) from
      tblpms where CaseNo = 'TR13-011-CRW')
      ) as t3

      on t1.CaseNo COLLATE DATABASE_DEFAULT = t2.CaseNo COLLATE DATABASE_DEFAULT

      and CaseNo = 'TR13-011-CRW'
通常在与LIKE匹配时不需要较低的值,因为它是

此外,当在字符串中间进行搜索时,例如'%%,%,不使用索引。顺便问一下,你有索引吗?您是否尝试过构建查询计划

正如我所看到的,这个问题几乎没有可能的解决方案:

添加另一个索引字段并在查询前填充一次:

更新tblpms set tr13\U flag=1,其中案例号类似于“%tr13-011%”

然后用where tr13_flag=1替换每个where LOWERCESENO(如“%tr13-011%”语句

上面的一个变化:使用索引,这样可以避免手动计算标志

将所有有价值的行抓取到临时表中一次:

挑选* 进入tr13011 来自tblpms,其中小写字母类似“%tr13-011%”

并加入其中,可能会在感兴趣的字段上添加索引

此外,您还可以将castnexteval连续转换为datetime。使用答案中的第四个版本,同时将其转换到临时表中

这个条件对我来说似乎没用:lowermrprocedure不为null,castnexteval为datetime。为什么不简单,并且mrprocedure不为空

版本4的示例

我不知道为什么需要COLLATE DATABASE_DEFAULT,因为它总是同一个表。 逻辑对我来说不是很清楚,但似乎产生了相同的结果,同时可读性更强,可能表现更好:

select
    CaseNo,
    mrprocedure,
    cast(mrdate as datetime) as mrdate,
    cast(nexteval as datetime) as nexteval,
    pmskey,
    mrtype,
    med_stat,
    clincontact,
    modfby,
    createdby,
    ppihandler
    -- other required fields from tblpms table
into #tr13
from tblpms where CaseNo = 'TR13-011-CRW'

declare @max_mrdate datetime
declare @max_nexteval datetime

select @max_mrdate=max(mrdate), @max_nexteval=max(nexteval) from #tr13

SELECT
      max(t2.pmskey) as pmskey, max(cast(t1.recdate as datetime)) as recdate,
      max(t2.mrtype) as mrtype, max(cast(t1.mrdate as datetime)) as mrdate,
      max(t2.CaseNo) as CaseNo, max(t2.pmclin) as pmclin,
      max(cast(t1.nexteval as datetime)) as nexteval,
      max(cast(t1.repdate as datetime)) as repdate,
      max(t2.mrprocedure) as mrprocedure, max(t2.med_stat) as med_stat,
      max(cast(t1.med_stateff as datetime)) as med_stateff,
      max(t2.clincontact) as clincontact,
      max(cast(t1.datemodf as datetime)) as datemodf, max(t2.modfby) as modfby,
      max(cast(t1.inceptiondate as datetime)) as inceptiondate,
      max(t2.createdby) as createdby, max(cast(t1.date_ent as datetime)) as date_ent,
      max(t2.ppihandler) as ppihandler

 FROM tblpms as t1
 JOIN #tr13 t2 on t1.CaseNo = t2.CaseNo
where
    t2.mrdate = @max_mrdate
    and t2.mrprocedure is not null
    and t2.nexteval = @max_nexteval

drop table #tr13
更新 我们设法将其简化为以下代码:

declare @max_mrdate datetime
declare @max_nexteval datetime

select
    @max_mrdate=max(cast(mrdate as datetime)),
    @max_nexteval=max(cast(nexteval as datetime))
from tblpms
where CaseNo = 'TR13-011-CRW'

select *
from tblpms where CaseNo = 'TR13-011-CRW'
and (
    cast(mrdate as datetime) = @max_mrdate
    or
    (mrprocedure is not null and cast(nexteval as datetime) = @max_nexteval)
)
通常在与LIKE匹配时不需要较低的值,因为它是

此外,当在字符串中间进行搜索时,例如'%%,%,不使用索引。顺便问一下,你有索引吗?您是否尝试过构建查询计划

正如我所看到的,这个问题几乎没有可能的解决方案:

添加另一个索引字段并在查询前填充一次:

更新tblpms set tr13\U flag=1,其中案例号类似于“%tr13-011%”

然后用where tr13_flag=1替换每个where LOWERCESENO(如“%tr13-011%”语句

上面的一个变化:使用索引,这样可以避免手动计算标志

将所有有价值的行抓取到临时表中一次:

挑选* 进入tr13011 来自tblpms,其中小写字母类似“%tr13-011%”

并加入其中,可能会在感兴趣的字段上添加索引

此外,您还可以将castnexteval连续转换为datetime。使用答案中的第四个版本,同时将其转换到临时表中

这个条件对我来说似乎没用:lowermrprocedure不为null,castnexteval为datetime。为什么不简单,并且mrprocedure不为空

版本4的示例

我不知道为什么需要COLLATE DATABASE_DEFAULT,因为它总是同一个表。 逻辑对我来说不是很清楚,但似乎产生了相同的结果,同时可读性更强,可能表现更好:

select
    CaseNo,
    mrprocedure,
    cast(mrdate as datetime) as mrdate,
    cast(nexteval as datetime) as nexteval,
    pmskey,
    mrtype,
    med_stat,
    clincontact,
    modfby,
    createdby,
    ppihandler
    -- other required fields from tblpms table
into #tr13
from tblpms where CaseNo = 'TR13-011-CRW'

declare @max_mrdate datetime
declare @max_nexteval datetime

select @max_mrdate=max(mrdate), @max_nexteval=max(nexteval) from #tr13

SELECT
      max(t2.pmskey) as pmskey, max(cast(t1.recdate as datetime)) as recdate,
      max(t2.mrtype) as mrtype, max(cast(t1.mrdate as datetime)) as mrdate,
      max(t2.CaseNo) as CaseNo, max(t2.pmclin) as pmclin,
      max(cast(t1.nexteval as datetime)) as nexteval,
      max(cast(t1.repdate as datetime)) as repdate,
      max(t2.mrprocedure) as mrprocedure, max(t2.med_stat) as med_stat,
      max(cast(t1.med_stateff as datetime)) as med_stateff,
      max(t2.clincontact) as clincontact,
      max(cast(t1.datemodf as datetime)) as datemodf, max(t2.modfby) as modfby,
      max(cast(t1.inceptiondate as datetime)) as inceptiondate,
      max(t2.createdby) as createdby, max(cast(t1.date_ent as datetime)) as date_ent,
      max(t2.ppihandler) as ppihandler

 FROM tblpms as t1
 JOIN #tr13 t2 on t1.CaseNo = t2.CaseNo
where
    t2.mrdate = @max_mrdate
    and t2.mrprocedure is not null
    and t2.nexteval = @max_nexteval

drop table #tr13
更新 我们设法将其简化为以下代码:

declare @max_mrdate datetime
declare @max_nexteval datetime

select
    @max_mrdate=max(cast(mrdate as datetime)),
    @max_nexteval=max(cast(nexteval as datetime))
from tblpms
where CaseNo = 'TR13-011-CRW'

select *
from tblpms where CaseNo = 'TR13-011-CRW'
and (
    cast(mrdate as datetime) = @max_mrdate
    or
    (mrprocedure is not null and cast(nexteval as datetime) = @max_nexteval)
)

这个答案很好。。它将我的查询速度从0.09+提高到了0.07+。。但是关于加入和其他问题呢?如果你在问题中加入实际的查询计划,这会很有帮助。我只需编辑我的帖子,添加我当前的查询,看看它会有什么不同。我可以做第二件事吗?我应该在数据库服务器中创建一个新列吗?还是应该在select查询中创建它?另外,你能给我举一个使用数字3和4的例子吗?感谢会员2不是一个好的解决方案。检查更新并坚持使用版本4,因为您不再使用像%这样的版本。这个答案很好。。它将我的查询速度从0.09+提高到了0.07+。。但是关于加入和其他问题呢?如果你在问题中加入实际的查询计划,这会很有帮助。我只需编辑我的帖子,添加我当前的查询,看看它会有什么不同。我可以做第二件事吗?我应该在数据库服务器中创建一个新列吗?还是应该在select查询中创建它?另外,你能给我举一个使用数字3和4的例子吗?感谢会员2不是一个好的解决方案。检查更新并坚持使用版本4,因为您不再使用LIKE%。