| |

VerySource

 Forgot password?
 Register
Search
View: 879|Reply: 7

Optimization: tens of thousands of query statements with parameters

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-25 23:40:01
| Show all posts |Read mode
The task at hand can be simplified to the following model:

declare @i int, @j int, @iCount int
set @i = 0
set @j = 0

while (@i <100)
begin
    while (@j <100)
    begin
        set @iCount = (select count (*) from table1 where field1 = @i and field2 = @j)
        if (@iCount <> 0)
        begin
            insert into table2 (field1, field2) values ​​(@i, @j)
        end
        set @j = @j +1
    end
set @i = @i +1
end

Can I achieve the best efficiency through batch execution or combining multiple select statements into one statement?
Reply

Use magic Report

0

Threads

211

Posts

108.00

Credits

Newbie

Rank: 1

Credits
108.00

 China

Post time: 2020-2-16 12:45:01
| Show all posts
--try
declare @i int, @j int, @iCount int
set @i = 0
set @j = 0

while (@i <100)
begin
    while (@j <100)
    begin
        insert into table2 (field1, field2)
select @i, @j
where exists (select 1 from table1 where field1 = @i and field2 = @j)

        set @j = @j +1
    end
set @i = @i +1
end
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-2-18 09:45:02
| Show all posts
insert into table2 (field1, field2)
select field1, field2 from table1
WHERE field1 <100 AND field2 <100 GROUP BY field1, field2 HAVING count (1)> 0
Reply

Use magic Report

0

Threads

16

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-2-18 13:30:02
| Show all posts
The solution is upstairs.
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-5-1 12:00:01
| Show all posts
Thank you, brothers. In fact, my intention was to ask about optimization using temporary tables, batch execution, etc., because this model is too simplified and may not fully simulate the prototype. The select statement in the prototype should not be easy to merge For example, the following writing:
select count (*) from table1 where field1 = 2 * @ i and field2 = @ j + 5
or
select count (*) from table1 where field1 = random number * @ i and field2 = random number * @ j

What I want to ask is how to optimize in this case? Thank you all.
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-5-4 18:15:01
| Show all posts
The first can be transformed as follows:
insert into table2 (field1, field2)
select field1, field2 from table1
WHERE field1 <100 * 2 AND field2 <100 + 5 AND field2> = 5
AND field1% 2 = 0
GROUP BY field1, field2
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 Italy

Post time: 2020-5-4 20:15:01
| Show all posts
Second, if random numbers are generated each time, there is nothing to optimize!
Reply

Use magic Report

0

Threads

126

Posts

73.00

Credits

Newbie

Rank: 1

Credits
73.00

 China

Post time: 2020-5-4 22:00:01
| Show all posts
Group can be removed. . .
As long as there are records, COUNT (*)> 0 can meet
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list