zoukankan      html  css  js  c++  java
  • sql中类型转换涉及的性能差异之convert和cast

    五六个表多次连接,数据仅有200多条时

    1.用convert把datetime转换成date

    WITH Virtual_T AS
                            (
                                SELECT 
                                        a.[ID]
                                        ,a.[CityID]
                                        ,(select CityName from SlCity where ID=a.[CityID]) as CityName
                                        ,(select CityDistrictID from SlCity where ID=a.[CityID]) as CityDistrictID
                                        ,a.[ContractNo]
                                        ,a.[ContractSignedDate]
                                        ,a.[TransactionDate]
                                        ,a.[HousesProcedureDate]
                                        ,a.[Customer]
                                        ,a.[BuildingName]
                                        ,a.[HouseProperties]
                                        ,a.[HousingContractAmount]
                                        ,a.[DealPrice]
                                        ,a.[MarketingPrice]
                                        ,a.[PayType]
                                        ,a.[ExecuteScheduleRatio]
                                        ,a.[PropertyAddress]
                                        ,a.[RoomNo]
                                        ,a.[PropertyType]
                                        ,a.[BuidingType]
                                        ,a.[Decoration]
                                        ,a.[BuidingArea]
                                        ,a.[UnitPrice]
                                        ,a.[ApartmentLayout]
                                        ,a.[Seller]
                                        ,a.[ContactInfo]
                                        ,a.[CreatorID]
                                        ,b.[CreateDate]
                                        ,a.[EditDate]
                                        ,a.[IsDelete]
                                        ,b.PaymentOfWeek
                                        ,b.PaymentSummary
                                        ,b.ExtraHouseAmount
                                        ,b.PaymentOfNextWeek
                                        ,b.ProgressOfWeek
                                        ,b.Remark
                                        ,b.EditDate AS WeekEditDate
                                        ,(select  min(StatDate)  from [dbo].[SlReportWeekDefine] where LiSuMeiWeek =(select lisumeiweek from [SlReportWeekDefine] where StatDate=CONVERT(char(10),b.createDate,120))) as 'start'
                                        ,(select  max(StatDate) from [dbo].[SlReportWeekDefine] where LiSuMeiWeek = (select lisumeiweek from [SlReportWeekDefine] where StatDate=CONVERT(char(10),b.createDate,120)))  as 'end'
                                        ,(select LiSuMeiWeek from [SlReportWeekDefine] where StatDate=CONVERT(char(10),b.createDate,120)) as LiSuMeiWeek
                                FROM [SlReportMortgageDetail] a WITH (NOLOCK)
                                JOIN [SlReportMortgageDetailOfWeek] b ON a.ID=b.DetailID
                                WHERE a.IsDelete=0 AND b.IsDelete=0 
                            ),T AS
                            (
                                SELECT a.*,b.CityDistrictName,ROW_NUMBER() OVER (ORDER BY a.CreateDate desc) AS [RowNumber],(Case LiSuMeiWeek when '200001'then '1' else '0' end) as IsCurrentWeek FROM Virtual_T a 
                                JOIN SlCityDistrict b ON a.CityDistrictID=b.ID
                                WHERE 1=1 
                            )
                            SELECT * FROM T 

    用时:

    2.用cast把datetime转换成date

        WITH Virtual_T AS
                            (
                                SELECT 
                                        a.[ID]
                                        ,a.[CityID]
                                        ,(select CityName from SlCity where ID=a.[CityID]) as CityName
                                        ,(select CityDistrictID from SlCity where ID=a.[CityID]) as CityDistrictID
                                        ,a.[ContractNo]
                                        ,a.[ContractSignedDate]
                                        ,a.[TransactionDate]
                                        ,a.[HousesProcedureDate]
                                        ,a.[Customer]
                                        ,a.[BuildingName]
                                        ,a.[HouseProperties]
                                        ,a.[HousingContractAmount]
                                        ,a.[DealPrice]
                                        ,a.[MarketingPrice]
                                        ,a.[PayType]
                                        ,a.[ExecuteScheduleRatio]
                                        ,a.[PropertyAddress]
                                        ,a.[RoomNo]
                                        ,a.[PropertyType]
                                        ,a.[BuidingType]
                                        ,a.[Decoration]
                                        ,a.[BuidingArea]
                                        ,a.[UnitPrice]
                                        ,a.[ApartmentLayout]
                                        ,a.[Seller]
                                        ,a.[ContactInfo]
                                        ,a.[CreatorID]
                                        ,b.[CreateDate]
                                        ,a.[EditDate]
                                        ,a.[IsDelete]
                                        ,b.PaymentOfWeek
                                        ,b.PaymentSummary
                                        ,b.ExtraHouseAmount
                                        ,b.PaymentOfNextWeek
                                        ,b.ProgressOfWeek
                                        ,b.Remark
                                        ,b.EditDate AS WeekEditDate
                                        ,(select  min(StatDate)  from [dbo].[SlReportWeekDefine] where LiSuMeiWeek =(select lisumeiweek from [SlReportWeekDefine] where StatDate=cast(b.CreateDate as date))) as 'start'
                                        ,(select  max(StatDate) from [dbo].[SlReportWeekDefine] where LiSuMeiWeek = (select lisumeiweek from [SlReportWeekDefine] where StatDate=cast(b.CreateDate as date)))  as 'end'
                                        ,(select LiSuMeiWeek from [SlReportWeekDefine] where StatDate=cast(b.CreateDate as date)) as LiSuMeiWeek
                                FROM [SlReportMortgageDetail] a WITH (NOLOCK)
                                JOIN [SlReportMortgageDetailOfWeek] b ON a.ID=b.DetailID
                                WHERE a.IsDelete=0 AND b.IsDelete=0 
                            ),T AS
                            (
                                SELECT a.*,b.CityDistrictName,ROW_NUMBER() OVER (ORDER BY a.CreateDate desc) AS [RowNumber],(Case LiSuMeiWeek when '200001'then '1' else '0' end) as IsCurrentWeek FROM Virtual_T a 
                                JOIN SlCityDistrict b ON a.CityDistrictID=b.ID
                                WHERE 1=1 
                            )
                            SELECT * FROM T 

    结果用时:

    结论:cast性能在转换date类型完爆convert,convert灵活性、类型适用性更广,更强大,所以性能损耗大;cast能满足使用的情况下,不用convert;程序能执行的情况下,避免在sql中使用内置函数

  • 相关阅读:
    浏览器 显示一个对话框,对话框中包含一条文字信息,用来提示用户输入文字。window.prompt()
    JS字符串转换为JSON对象的四种方法
    js中 json对象与json字符串相互转换的几种方式 $.parseJSON(jsonStr)json字符串转换为json对象
    C#变量命名规则(命名规范)
    C#中AppDomain.CurrentDomain.BaseDirectory(获取程序的基目录)及各种路径获取方法
    C# 的 Path.GetFileName、Path.GetExtension、Path.GetDirectoryName千一网络 http://www.itpow.com/
    js计算两个时间相差天数,获取时间的毫秒数之差
    C#将DataTable转化为List<T>
    https://www.cnblogs.com/sizhizhiyue/p/4820973.html asp.net后台导出excel的方法一:使用response导出excel
    .NET调用AS/400上的程序(.NET CALL AS/400 PGM)
  • 原文地址:https://www.cnblogs.com/liuchuanxu/p/4488087.html
Copyright © 2011-2022 走看看