Home All Groups Group Topic Archive Search About
Author
27 Jul 2006 12:09 PM
Marco
hello all,

this is my request:
declare @id_min_inscrit int
declare @id_max_inscrit int

select @id_min_alerte = min(cast(IDas int)) from TABLE
select @id_max_alerte=  max(cast(ID as int)) from TABLE

set @cpt_id =  @id_min_alerte

while @cpt_id < = @id_max_alerte
Begin

  set @cpt_id=@cpt_id+1
end

the problem it's very long 3 minutes!!!(40000 lines!!!)
why , is thre a way to optimize?
because i want to add any requests for traitment.

thanks in advance for your help

Author
27 Jul 2006 12:12 PM
Marco
if i add
select cast([ID] as int  from IMPORT_AVAL_2 with(nolock) where  cast(id as
int)=@cpt_id

             Begin
here -> 
             set @cpt_id=@cpt_id+1
             end

6 minutes!!!!

Show quote
"Marco" wrote:

> hello all,
>
> this is my request:
> declare @id_min_inscrit int
> declare @id_max_inscrit int
>
> select @id_min_alerte = min(cast(IDas int)) from TABLE
> select @id_max_alerte=  max(cast(ID as int)) from TABLE
>
> set @cpt_id =  @id_min_alerte
>
> while @cpt_id < = @id_max_alerte
> Begin
>
>   set @cpt_id=@cpt_id+1
> end
>
> the problem it's very long 3 minutes!!!(40000 lines!!!)
> why , is thre a way to optimize?
> because i want to add any requests for traitment.
>
> thanks in advance for your help
>
>
>
>
>
>
>
>
Author
27 Jul 2006 12:18 PM
Omnibuzz
Can you tell us what your requirement is?


> > while @cpt_id < = @id_max_alerte
> > Begin
> >
> >   set @cpt_id=@cpt_id+1
> > end
> >
What is the use of this while loop?
What do you want to achieve?
Author
27 Jul 2006 12:18 PM
Aaron Bertrand [SQL Server MVP]
> declare @id_min_inscrit int
> declare @id_max_inscrit int
>
> select @id_min_alerte = min(cast(IDas int)) from TABLE
> select @id_max_alerte=  max(cast(ID as int)) from TABLE

What data type is ID?  Is the CAST necessary?  Is there an index?  Why do
this in two queries when you can do it in one?

SELECT
    @id_min_alerte = MIN(ID),
    @id_max_alerte = MAX(ID)
FROM
    TABLE;

Then I'm not sure I understand the loop.  What are you counting / adding?

A
Author
27 Jul 2006 12:19 PM
Dan Guzman
What data type is IDas?  It seems you could do something like:

SELECT CAST(MAX(IDas) - MIN(IDas) + 1 AS int)
FROM dbo.MyTable

--
Hope this helps.

Dan Guzman
SQL Server MVP

Show quote
"Marco" <Ma***@discussions.microsoft.com> wrote in message
news:106094C6-DFA9-4FC6-8007-DE2AE58E5659@microsoft.com...
> hello all,
>
> this is my request:
> declare @id_min_inscrit int
> declare @id_max_inscrit int
>
> select @id_min_alerte = min(cast(IDas int)) from TABLE
> select @id_max_alerte=  max(cast(ID as int)) from TABLE
>
> set @cpt_id =  @id_min_alerte
>
> while @cpt_id < = @id_max_alerte
> Begin
>
>  set @cpt_id=@cpt_id+1
> end
>
> the problem it's very long 3 minutes!!!(40000 lines!!!)
> why , is thre a way to optimize?
> because i want to add any requests for traitment.
>
> thanks in advance for your help
>
>
>
>
>
>
>
>
Author
27 Jul 2006 12:21 PM
Uri Dimant
Marco
Can yopu elaborate a little bit?
> select @id_min_alerte = min(cast(IDas int)) from TABLE
> select @id_max_alerte=  max(cast(ID as int)) from TABLE

select @id_min_alerte = min(cast(IDas int)),@id_max_alerte=  max(cast(ID as
int))   from TABLE




Show quote
"Marco" <Ma***@discussions.microsoft.com> wrote in message
news:106094C6-DFA9-4FC6-8007-DE2AE58E5659@microsoft.com...
> hello all,
>
> this is my request:
> declare @id_min_inscrit int
> declare @id_max_inscrit int
>
> select @id_min_alerte = min(cast(IDas int)) from TABLE
> select @id_max_alerte=  max(cast(ID as int)) from TABLE
>
> set @cpt_id =  @id_min_alerte
>
> while @cpt_id < = @id_max_alerte
> Begin
>
>  set @cpt_id=@cpt_id+1
> end
>
> the problem it's very long 3 minutes!!!(40000 lines!!!)
> why , is thre a way to optimize?
> because i want to add any requests for traitment.
>
> thanks in advance for your help
>
>
>
>
>
>
>
>
>
Author
27 Jul 2006 12:24 PM
Roy Harvey
Transact-SQL, the dialect of SQL used by Microsoft SQL Server, is an
efficient tool for relational processing.  It is not well optimized
for looping and processing things one row at a time.

There is an excellent chance that the part you did not include - the
rest of what happens in the loop - can be rewritten so that you do not
need any loop.  Stated another way, the work can probably be done
relationally, processing all the rows in a single command, instead of
procedurally, one row at a time as in the sample code.  If you explain
what needs to be done in that code someone here may be able to give
some constructive advice.  For a better idea of what to include, see
http://www.aspfaq.com/5006.

Roy Harvey
Beacon Falls, CT

On Thu, 27 Jul 2006 05:09:02 -0700, Marco
<Ma***@discussions.microsoft.com> wrote:

Show quote
>hello all,
>
>this is my request:
>declare @id_min_inscrit int
>declare @id_max_inscrit int
>
>select @id_min_alerte = min(cast(IDas int)) from TABLE
>select @id_max_alerte=  max(cast(ID as int)) from TABLE
>
>set @cpt_id =  @id_min_alerte
>
>while @cpt_id < = @id_max_alerte
>Begin
>
>  set @cpt_id=@cpt_id+1
>end
>
>the problem it's very long 3 minutes!!!(40000 lines!!!)
>why , is thre a way to optimize?
>because i want to add any requests for traitment.
>
>thanks in advance for your help
Author
27 Jul 2006 12:32 PM
Marco
thanks for your response for all

i have atable which contains 40000 lines but begins with an ID =120 and
finished by 250000 for example
i loop all line for get information and effectuate a traitment
if col001= ... begin... end

the problem it is veruy long

PS: i convert because Id on my table is varchar and my counter is int that's
why i made the conversion


Show quote
"Roy Harvey" wrote:

> Transact-SQL, the dialect of SQL used by Microsoft SQL Server, is an
> efficient tool for relational processing.  It is not well optimized
> for looping and processing things one row at a time.
>
> There is an excellent chance that the part you did not include - the
> rest of what happens in the loop - can be rewritten so that you do not
> need any loop.  Stated another way, the work can probably be done
> relationally, processing all the rows in a single command, instead of
> procedurally, one row at a time as in the sample code.  If you explain
> what needs to be done in that code someone here may be able to give
> some constructive advice.  For a better idea of what to include, see
> http://www.aspfaq.com/5006.
>
> Roy Harvey
> Beacon Falls, CT
>
> On Thu, 27 Jul 2006 05:09:02 -0700, Marco
> <Ma***@discussions.microsoft.com> wrote:
>
> >hello all,
> >
> >this is my request:
> >declare @id_min_inscrit int
> >declare @id_max_inscrit int
> >
> >select @id_min_alerte = min(cast(IDas int)) from TABLE
> >select @id_max_alerte=  max(cast(ID as int)) from TABLE
> >
> >set @cpt_id =  @id_min_alerte
> >
> >while @cpt_id < = @id_max_alerte
> >Begin
> >
> >  set @cpt_id=@cpt_id+1
> >end
> >
> >the problem it's very long 3 minutes!!!(40000 lines!!!)
> >why , is thre a way to optimize?
> >because i want to add any requests for traitment.
> >
> >thanks in advance for your help
>
Author
27 Jul 2006 12:46 PM
Omnibuzz
Can you give the ddl, sample data and result you need with a small
explaination on the calculation that you perform. Maybe you need to loop
through it line by line
--
-Omnibuzz (The SQL GC)

http://omnibuzz-sql.blogspot.com/
Author
27 Jul 2006 12:51 PM
Aaron Bertrand [SQL Server MVP]
> Maybe you need to loop through it line by line

Did you mean "Maybe you /don't/ need to loop ..."?
Author
27 Jul 2006 1:10 PM
Omnibuzz
oops.. of course.. Thats what I meant :)
--
-Omnibuzz (The SQL GC)

http://omnibuzz-sql.blogspot.com/



Show quote
"Aaron Bertrand [SQL Server MVP]" wrote:

> > Maybe you need to loop through it line by line
>
> Did you mean "Maybe you /don't/ need to loop ..."?
>
>
>
Author
27 Jul 2006 1:13 PM
Erland Sommarskog
Marco (Ma***@discussions.microsoft.com) writes:
> thanks for your response for all
>
> i have atable which contains 40000 lines but begins with an ID =120 and
> finished by 250000 for example
> i loop all line for get information and effectuate a traitment
> if col001= ... begin... end
>
> the problem it is veruy long
>
> PS: i convert because Id on my table is varchar and my counter is int
> that's why i made the conversion

When you say: 

  select cast([ID] as int  from IMPORT_AVAL_2 with(nolock)
  where  cast(id as int)=@cpt_id

and there is an index on id, that index helps you none if you cast the
varchar to int, because character data and index data sorts differently.
That's a scan instead of index seek. There you have a good deal of
explanation of why it takes time. Say

    WHERE id = CAST (@cpt_id AS varchar)

instead. Then again, this can fail if there are leading zeroes etc.

Anyway, normally in SQL programming you don't use loops - you process all
rows at once. When you process rows one by one, it usually takes a lot
more time. If there are 40 rows you can get away with it, but for 40000 rows
you will have to pay dearly.

But if you don't tell us what is going on in this loop, we cannot assist
you with knowing what you are doing. You could at least start with posting
the full code of the loop.

And please in keep in mind that we know SQL in this place, we know very
little about your business problem. Simply saying "traitment" just leaves
in the dark.

--
Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Author
27 Jul 2006 12:45 PM
Marco
that's unbelievable is a other traitmeent with the same code
is ok with 60000 lines in 2-3 minutes
the tables are the same type
the code is the same
no index in two tables

why this difference with the time?


Show quote
"Marco" wrote:

> hello all,
>
> this is my request:
> declare @id_min_inscrit int
> declare @id_max_inscrit int
>
> select @id_min_alerte = min(cast(IDas int)) from TABLE
> select @id_max_alerte=  max(cast(ID as int)) from TABLE
>
> set @cpt_id =  @id_min_alerte
>
> while @cpt_id < = @id_max_alerte
> Begin
>
>   set @cpt_id=@cpt_id+1
> end
>
> the problem it's very long 3 minutes!!!(40000 lines!!!)
> why , is thre a way to optimize?
> because i want to add any requests for traitment.
>
> thanks in advance for your help
>
>
>
>
>
>
>
>
Author
27 Jul 2006 12:50 PM
Aaron Bertrand [SQL Server MVP]
You need to show us all of the code.  I highly doubt it takes SQL Server
more than two minutes to increment a counter by 1 40,000 times.  On my
desktop SQL Server can perform this task in 14 seconds.  So, what else
you're doing in that loop is probably the most important part!

I also think you need to show us the actual structure of the tables,
incuding data types, keys, indexes, constraints -- possibly even triggers,
if the missing code touches the table within the loop.

A



Show quote
"Marco" <Ma***@discussions.microsoft.com> wrote in message
news:2079B48D-DCAF-4EA2-9506-BA93656AE75C@microsoft.com...
> that's unbelievable is a other traitmeent with the same code
> is ok with 60000 lines in 2-3 minutes
> the tables are the same type
> the code is the same
> no index in two tables
>
> why this difference with the time?
>
>
> "Marco" wrote:
>
>> hello all,
>>
>> this is my request:
>> declare @id_min_inscrit int
>> declare @id_max_inscrit int
>>
>> select @id_min_alerte = min(cast(IDas int)) from TABLE
>> select @id_max_alerte=  max(cast(ID as int)) from TABLE
>>
>> set @cpt_id =  @id_min_alerte
>>
>> while @cpt_id < = @id_max_alerte
>> Begin
>>
>>   set @cpt_id=@cpt_id+1
>> end
>>
>> the problem it's very long 3 minutes!!!(40000 lines!!!)
>> why , is thre a way to optimize?
>> because i want to add any requests for traitment.
>>
>> thanks in advance for your help
>>
>>
>>
>>
>>
>>
>>
>>

AddThis Social Bookmark Button