zoukankan      html  css  js  c++  java
  • 一次简单的sql性能比较

    用普通的 while循环插入数据

    执行5分钟(未执行完,并且已经报内存溢出的错误。)

    declare @feesCount bigint;
    declare @feesid bigint
    set @feesCount=1
    set @feesid=100201000000002422
    
    while(@feesCount<=100000)
    begin
    INSERT INTO [dbo].[TF_Fees]
               ([FeesID]
               ,[CommID]
               ,[CustID]
               ,[RoomID]
               ,[CostID]
               ,[CorpStanID]
               ,[StanID]
               ,[HandID]
               ,[ParkID]
               ,[CarparkID]
               ,[MeterSign]
               ,[AccountsDueDate]
               ,[FeesDueDate]
               ,[FeesStateDate]
               ,[FeesEndDate]
               ,[DueAmount]
               ,[DebtsAmount]
               ,[WaivAmount]
               ,[PrecAmount]
               ,[PaidAmount]
               ,[RefundAmount]
               ,[ContID]
               ,[LeaseContID]
               ,[OwnerFeesID]
               ,[IsAudit]
               ,[IsBank]
               ,[IsCharge]
               ,[IsFreeze]
               ,[IsProperty]
               ,[IsPrec]
               ,[CalcAmount]
               ,[CalcAmount2]
               ,[IncidentID]
               ,[StanMemo]
               ,[CommisionCostID]
               ,[CommisionAmount]
               ,[WaivCommisAmount]
               ,[PerStanAmount]
               ,[AccountFlag]
               ,[FeesMemo]
               ,[MeterID]
               ,[PMeterID]
               ,[CsmTaskID])
        select 
                @feesid+@feesCount as [FeesID]
               ,600  as [CommID]
               ,[CustID]
               ,[RoomID]
               ,[CostID]
               ,[CorpStanID]
               ,[StanID]
               ,[HandID]
               ,[ParkID]
               ,[CarparkID]
               ,[MeterSign]
               ,[AccountsDueDate]
               ,[FeesDueDate]
               ,[FeesStateDate]
               ,[FeesEndDate]
               ,[DueAmount]
               ,[DebtsAmount]
               ,[WaivAmount]
               ,[PrecAmount]
               ,[PaidAmount]
               ,[RefundAmount]
               ,[ContID]
               ,[LeaseContID]
               ,[OwnerFeesID]
               ,[IsAudit]
               ,[IsBank]
               ,[IsCharge]
               ,[IsFreeze]
               ,[IsProperty]
               ,[IsPrec]
               ,[CalcAmount]
               ,[CalcAmount2]
               ,[IncidentID]
               ,[StanMemo]
               ,[CommisionCostID]
               ,[CommisionAmount]
               ,[WaivCommisAmount]
               ,[PerStanAmount]
               ,[AccountFlag]
               ,[FeesMemo]
               ,[MeterID]
               ,[PMeterID]
               ,[CsmTaskID]
        from TF_Fees
        where FeesID=@feesid
        
        set @feesCount=@feesCount+1;
    end

    改用CTE后

    declare @feesCount bigint;
    declare @feesid bigint
    set @feesCount=1
    select  @feesid=MAX(feesid) from TF_Fees
    
    Create TABLE #temp (
        [FeesID] [bigint] NOT NULL,
        [CommID] [int] NULL,
        [CustID] [bigint] NULL,
        [RoomID] [bigint] NULL,
        [CostID] [bigint] NULL,
        [CorpStanID] [bigint] NULL,
        [StanID] [bigint] NULL,
        [HandID] [bigint] NULL,
        [ParkID] [bigint] NULL,
        [CarparkID] [bigint] NULL,
        [MeterSign] [nvarchar](10) NULL,
        [AccountsDueDate] [datetime] NULL,
        [FeesDueDate] [datetime] NULL,
        [FeesStateDate] [datetime] NULL,
        [FeesEndDate] [datetime] NULL,
        [DueAmount] [numeric](18, 2) NULL,
        [DebtsAmount] [numeric](18, 2) NULL,
        [WaivAmount] [numeric](18, 2) NULL,
        [PrecAmount] [numeric](18, 2) NULL,
        [PaidAmount] [numeric](18, 2) NULL,
        [RefundAmount] [numeric](18, 2) NULL,
        [ContID] [bigint] NULL,
        [LeaseContID] [bigint] NULL,
        [OwnerFeesID] [bigint] NULL,
        [IsAudit] [smallint] NULL,
        [IsBank] [smallint] NULL,
        [IsCharge] [smallint] NULL,
        [IsFreeze] [smallint] NULL,
        [IsProperty] [smallint] NULL,
        [IsPrec] [smallint] NULL,
        [CalcAmount] [numeric](18, 2) NULL,
        [CalcAmount2] [numeric](18, 2) NULL,
        [IncidentID] [bigint] NULL,
        [StanMemo] [nvarchar](200) NULL,
        [CommisionCostID] [bigint] NULL,
        [CommisionAmount] [numeric](18, 2) NULL,
        [WaivCommisAmount] [numeric](18, 2) NULL,
        [PerStanAmount] [numeric](18, 2) NULL,
        [AccountFlag] [int] NULL,
        [FeesMemo] [nvarchar](255) NULL,
        [MeterID] [bigint] NULL,
        [PMeterID] [bigint] NULL,
        [CsmTaskID] [uniqueidentifier] NULL)
    
    print @feesCount
    while(@feesCount<=100000)
    begin
    INSERT INTO #temp
               ([FeesID]
               ,[CommID]
               ,[CustID]
               ,[RoomID]
               ,[CostID]
               ,[CorpStanID]
               ,[StanID]
               ,[HandID]
               ,[ParkID]
               ,[CarparkID]
               ,[MeterSign]
               ,[AccountsDueDate]
               ,[FeesDueDate]
               ,[FeesStateDate]
               ,[FeesEndDate]
               ,[DueAmount]
               ,[DebtsAmount]
               ,[WaivAmount]
               ,[PrecAmount]
               ,[PaidAmount]
               ,[RefundAmount]
               ,[ContID]
               ,[LeaseContID]
               ,[OwnerFeesID]
               ,[IsAudit]
               ,[IsBank]
               ,[IsCharge]
               ,[IsFreeze]
               ,[IsProperty]
               ,[IsPrec]
               ,[CalcAmount]
               ,[CalcAmount2]
               ,[IncidentID]
               ,[StanMemo]
               ,[CommisionCostID]
               ,[CommisionAmount]
               ,[WaivCommisAmount]
               ,[PerStanAmount]
               ,[AccountFlag]
               ,[FeesMemo]
               ,[MeterID]
               ,[PMeterID]
               ,[CsmTaskID])
        select 
                @feesid+@feesCount as [FeesID]
               ,600  as [CommID]
               ,[CustID]
               ,[RoomID]
               ,[CostID]
               ,[CorpStanID]
               ,[StanID]
               ,[HandID]
               ,[ParkID]
               ,[CarparkID]
               ,[MeterSign]
               ,[AccountsDueDate]
               ,[FeesDueDate]
               ,[FeesStateDate]
               ,[FeesEndDate]
               ,[DueAmount]
               ,[DebtsAmount]
               ,[WaivAmount]
               ,[PrecAmount]
               ,[PaidAmount]
               ,[RefundAmount]
               ,[ContID]
               ,[LeaseContID]
               ,[OwnerFeesID]
               ,[IsAudit]
               ,[IsBank]
               ,[IsCharge]
               ,[IsFreeze]
               ,[IsProperty]
               ,[IsPrec]
               ,[CalcAmount]
               ,[CalcAmount2]
               ,[IncidentID]
               ,[StanMemo]
               ,[CommisionCostID]
               ,[CommisionAmount]
               ,[WaivCommisAmount]
               ,[PerStanAmount]
               ,[AccountFlag]
               ,[FeesMemo]
               ,[MeterID]
               ,[PMeterID]
               ,[CsmTaskID]
        from TF_Fees
        where FeesID=@feesid
    
        set @feesCount=@feesCount+1;
    end
    
    select * from #temp;
    
    disable trigger [TRG_TF_Fees_GetMaxFeesID] on TF_Fees
    insert into TF_Fees select * from #temp ;
    enable trigger [TRG_TF_Fees_GetMaxFeesID] on TF_Fees
    drop table #temp

    执行只用13秒。。。。。

  • 相关阅读:
    js 时差转换 getTimezoneOffset()
    js 验证对象是否为数组
    mac下的一些mysql操作
    mac下mysql 1045 (28000): Access denied for user 'root'@'localhost' (using password:
    canvas 画布 文字描边
    background-size: contain 与cover的区别,以及ie78的兼容写法
    tortoisegit 常见错误disconnected no supported authentication methods available(server sent: publickey)
    windows 下git 的配置安装与使用
    Spring中Bean的生命周期
    HBase--DependentColumnFilter(参考例过滤器 )详解
  • 原文地址:https://www.cnblogs.com/shikyoh/p/2879874.html
Copyright © 2011-2022 走看看