Showing posts with label query. Show all posts
Showing posts with label query. Show all posts

Wednesday, March 28, 2012

Receive all messages on queue

Hi i am trying to create a batch process then commit for all messages on the queue. The problem i am having is when i run my query (As below) I only receive the first message and the corresponding end dialog for the message although i have 2000 records sitting in the queue. It is my understanding that receive without any criteria i.e top(1) or where clause should select everything of the queue. I tried receive top(100) expecting 100 records but still only got 2 back.

any help appreciated.

WAITFOR(RECEIVE

queuing_order,

conversation_handle,

message_type_name,

message_body

FROM [RMIS_COMMS_Queue]

INTO @.tableMessages), TIMEOUT 2000;

Each RECEIVE returns only messages belonging to conversation in one conversation group only. If each conversation is its own group (which is true unless you use RELATED_CONVERSATION clause in BEGIN DIALOG or you use MOVE CONVERSATION) then it means you can RECEIVE only one conversation in one call. The TOP clause applies to this resultset (one conversation).

HTH,
~ Remus

|||Thanks for that. Can i just refer to your article on batch processing messages (the example with the cursor) this is the method i am try ing to implement but my scenario is many shops with many tills, where each message/conversation is a complete sale from a till i.e many conversations with one message. Is it safe to say this is not the best method for trying to speed this up, what would be the best way to process these messages, bearing in mind sometimes a shop can lose comms and send down a batch of sales when it reconnects.|||

I'm using triggers to cause an update of a seperate database via service broker, in our activated stored procedure we are seeing similiar situations, Could you give an example of a script that a trigger might use to take advantage of the RELATED_CONVERSATION clause? I looked at the documentation and didn't see how I could use this in our situation. (trigger on update sends inserted table with for xml clause as message to queue)

Thanks,

Bill

Receive all messages on queue

Hi i am trying to create a batch process then commit for all messages on the queue. The problem i am having is when i run my query (As below) I only receive the first message and the corresponding end dialog for the message although i have 2000 records sitting in the queue. It is my understanding that receive without any criteria i.e top(1) or where clause should select everything of the queue. I tried receive top(100) expecting 100 records but still only got 2 back.

any help appreciated.

WAITFOR(RECEIVE

queuing_order,

conversation_handle,

message_type_name,

message_body

FROM [RMIS_COMMS_Queue]

INTO @.tableMessages), TIMEOUT 2000;

Each RECEIVE returns only messages belonging to conversation in one conversation group only. If each conversation is its own group (which is true unless you use RELATED_CONVERSATION clause in BEGIN DIALOG or you use MOVE CONVERSATION) then it means you can RECEIVE only one conversation in one call. The TOP clause applies to this resultset (one conversation).

HTH,
~ Remus

|||Thanks for that. Can i just refer to your article on batch processing messages (the example with the cursor) this is the method i am try ing to implement but my scenario is many shops with many tills, where each message/conversation is a complete sale from a till i.e many conversations with one message. Is it safe to say this is not the best method for trying to speed this up, what would be the best way to process these messages, bearing in mind sometimes a shop can lose comms and send down a batch of sales when it reconnects.|||

I'm using triggers to cause an update of a seperate database via service broker, in our activated stored procedure we are seeing similiar situations, Could you give an example of a script that a trigger might use to take advantage of the RELATED_CONVERSATION clause? I looked at the documentation and didn't see how I could use this in our situation. (trigger on update sends inserted table with for xml clause as message to queue)

Thanks,

Bill

Monday, March 26, 2012

recbase.cpp:1374

Hello,
I have this error after a select query (wich works for years).
Serveur_: Msg 3624, Niveau 20, tat 1, Ligne 1
Location: recbase.cpp:1374
Expression: m_nVars > 0
SPID: 51
Process ID: 1188
Connexion interrompue
I made a checkdb and everything is Ok.
does anyone has an idea ?Maybe these articles:
http://support.microsoft.com/defaul...kb;en-us;311104
http://support.microsoft.com/defaul...kb;en-us;317089
can help you?
Dejan Sarka, SQL Server MVP
Associate Mentor
Solid Quality Learning
More than just Training
www.SolidQualityLearning.com
"Alexandra Bishop" <boccara@.netvision.net.il> wrote in message
news:9e09323a.0402110606.33a4dc1d@.posting.google.com...
> Hello,
> I have this error after a select query (wich works for years).
> Serveur : Msg 3624, Niveau 20, tat 1, Ligne 1
> Location: recbase.cpp:1374
> Expression: m_nVars > 0
> SPID: 51
> Process ID: 1188
> Connexion interrompue
> I made a checkdb and everything is Ok.
> does anyone has an idea ?

recbase.cpp:1374

Hello,
I have this error after a select query (wich works for years).
Serveur : Msg 3624, Niveau 20, État 1, Ligne 1
Location: recbase.cpp:1374
Expression: m_nVars > 0
SPID: 51
Process ID: 1188
Connexion interrompue
I made a checkdb and everything is Ok.
does anyone has an idea ?Maybe these articles:
http://support.microsoft.com/default.aspx?scid=kb;en-us;311104
http://support.microsoft.com/default.aspx?scid=kb;en-us;317089
can help you?
--
Dejan Sarka, SQL Server MVP
Associate Mentor
Solid Quality Learning
More than just Training
www.SolidQualityLearning.com
"Alexandra Bishop" <boccara@.netvision.net.il> wrote in message
news:9e09323a.0402110606.33a4dc1d@.posting.google.com...
> Hello,
> I have this error after a select query (wich works for years).
> Serveur : Msg 3624, Niveau 20, État 1, Ligne 1
> Location: recbase.cpp:1374
> Expression: m_nVars > 0
> SPID: 51
> Process ID: 1188
> Connexion interrompue
> I made a checkdb and everything is Ok.
> does anyone has an idea ?sql

recbase.cpp error

Can anyone help me with this error? The query that is causing this
problem was running like a champ up until about 4 days ago. I have done no
upgrades to the database server in the last couple weeks. Thanks.
Event Type: Error
Event Source: MSSQLSERVER
Event Category: (2)
Event ID: 17055
Date: 10/6/2003
Time: 12:08:48 PM
User: N/A
Computer: DATASERVERA
Description:
17066 :
SQL Server Assertion: File: <recbase.cpp>, line=1378
Failed Assertion = 'm_offBeginVar < m_SizeRec'.
-dougI found this in an old post...
This looks bad. An assertion error is a check that a programmer puts
into his code to verify that assumptions in his code are true. If they
are not, abortion is executed to prevent even more serious accidents.
(Data corruption is after all worse than a hanging or crashing server.)
Now, the assertion 'FALSE' is a little funny. Looks like a "we are not
supposed to come here".
If this happen to you more than once, I would recommend that you open a
case with Microsoft. They may know about the problem and have a workaround.
In any case, since this clearly is a bug in SQL Server, any fee that
you have to pay initially should be refunded.
--
HTH
Ryan Waight, MCDBA, MCSE
"Doug Threewitt" <doug@.seisystems.com> wrote in message
news:%23lK4EhDjDHA.488@.TK2MSFTNGP09.phx.gbl...
> Can anyone help me with this error? The query that is causing this
> problem was running like a champ up until about 4 days ago. I have done
no
> upgrades to the database server in the last couple weeks. Thanks.
> Event Type: Error
> Event Source: MSSQLSERVER
> Event Category: (2)
> Event ID: 17055
> Date: 10/6/2003
> Time: 12:08:48 PM
> User: N/A
> Computer: DATASERVERA
> Description:
> 17066 :
> SQL Server Assertion: File: <recbase.cpp>, line=1378
> Failed Assertion = 'm_offBeginVar < m_SizeRec'.
>
> -doug
>|||This usually points to hardware-caused corruption in your database. The
assert is basically saying that a record on a page is corrupt. You can
contact Product Support (http://support.microsoft.com) who will be able to
help or do the following:
1) run DBCC CHECKDB on the database to determine if any corruption is
present
2) check the NT event log and SQL Server errorlog for previous
errors/problems
3) run hardware diagnostics
4) if corruption is present, restore the database from your up-to-date
backups onto different hardware
Regards,
Paul.
--
Paul Randal
DBCC Technical Lead, Microsoft SQL Server Storage Engine
This posting is provided "AS IS" with no warranties, and confers no rights.
"Doug Threewitt" <doug@.seisystems.com> wrote in message
news:#lK4EhDjDHA.488@.TK2MSFTNGP09.phx.gbl...
> Can anyone help me with this error? The query that is causing this
> problem was running like a champ up until about 4 days ago. I have done
no
> upgrades to the database server in the last couple weeks. Thanks.
> Event Type: Error
> Event Source: MSSQLSERVER
> Event Category: (2)
> Event ID: 17055
> Date: 10/6/2003
> Time: 12:08:48 PM
> User: N/A
> Computer: DATASERVERA
> Description:
> 17066 :
> SQL Server Assertion: File: <recbase.cpp>, line=1378
> Failed Assertion = 'm_offBeginVar < m_SizeRec'.
>
> -doug
>|||I've run DBCC CHECKDB on the database and it found no corruption. The event
log shows this error occuring a few other times. What hardware diagnostics
do you recommend running? Also, how can I find my PID for the SQL Server so
I can get some help from Microsoft regarding this matter. Thanks for all
your help
-doug
"Paul S Randal [MS]" <prandal@.online.microsoft.com> wrote in message
news:%23gO4Z2OjDHA.2160@.TK2MSFTNGP12.phx.gbl...
> This usually points to hardware-caused corruption in your database. The
> assert is basically saying that a record on a page is corrupt. You can
> contact Product Support (http://support.microsoft.com) who will be able to
> help or do the following:
> 1) run DBCC CHECKDB on the database to determine if any corruption is
> present
> 2) check the NT event log and SQL Server errorlog for previous
> errors/problems
> 3) run hardware diagnostics
> 4) if corruption is present, restore the database from your up-to-date
> backups onto different hardware
> Regards,
> Paul.
> --
> Paul Randal
> DBCC Technical Lead, Microsoft SQL Server Storage Engine
> This posting is provided "AS IS" with no warranties, and confers no
rights.
> "Doug Threewitt" <doug@.seisystems.com> wrote in message
> news:#lK4EhDjDHA.488@.TK2MSFTNGP09.phx.gbl...
> > Can anyone help me with this error? The query that is causing this
> > problem was running like a champ up until about 4 days ago. I have done
> no
> > upgrades to the database server in the last couple weeks. Thanks.
> >
> > Event Type: Error
> > Event Source: MSSQLSERVER
> > Event Category: (2)
> > Event ID: 17055
> > Date: 10/6/2003
> > Time: 12:08:48 PM
> > User: N/A
> > Computer: DATASERVERA
> > Description:
> > 17066 :
> > SQL Server Assertion: File: <recbase.cpp>, line=1378
> > Failed Assertion = 'm_offBeginVar < m_SizeRec'.
> >
> >
> >
> > -doug
> >
> >
>|||Any disk, controller or IO subsystem diagnostics you can run. I don't know
about the PID.
There should also be some sqlXXXXX.dmp files in the same directory as the
errorlog which will be useful to PSS as they will have the call stacks at
the time the assert fires.
Regards,
Paul.
--
Paul Randal
DBCC Technical Lead, Microsoft SQL Server Storage Engine
This posting is provided "AS IS" with no warranties, and confers no rights.
"Doug Threewitt" <doug@.seisystems.com> wrote in message
news:#EAOl7QjDHA.2160@.TK2MSFTNGP10.phx.gbl...
> I've run DBCC CHECKDB on the database and it found no corruption. The
event
> log shows this error occuring a few other times. What hardware
diagnostics
> do you recommend running? Also, how can I find my PID for the SQL Server
so
> I can get some help from Microsoft regarding this matter. Thanks for all
> your help
>
> -doug
>
> "Paul S Randal [MS]" <prandal@.online.microsoft.com> wrote in message
> news:%23gO4Z2OjDHA.2160@.TK2MSFTNGP12.phx.gbl...
> > This usually points to hardware-caused corruption in your database. The
> > assert is basically saying that a record on a page is corrupt. You can
> > contact Product Support (http://support.microsoft.com) who will be able
to
> > help or do the following:
> > 1) run DBCC CHECKDB on the database to determine if any corruption is
> > present
> > 2) check the NT event log and SQL Server errorlog for previous
> > errors/problems
> > 3) run hardware diagnostics
> > 4) if corruption is present, restore the database from your up-to-date
> > backups onto different hardware
> >
> > Regards,
> >
> > Paul.
> >
> > --
> > Paul Randal
> > DBCC Technical Lead, Microsoft SQL Server Storage Engine
> >
> > This posting is provided "AS IS" with no warranties, and confers no
> rights.
> > "Doug Threewitt" <doug@.seisystems.com> wrote in message
> > news:#lK4EhDjDHA.488@.TK2MSFTNGP09.phx.gbl...
> > > Can anyone help me with this error? The query that is causing
this
> > > problem was running like a champ up until about 4 days ago. I have
done
> > no
> > > upgrades to the database server in the last couple weeks. Thanks.
> > >
> > > Event Type: Error
> > > Event Source: MSSQLSERVER
> > > Event Category: (2)
> > > Event ID: 17055
> > > Date: 10/6/2003
> > > Time: 12:08:48 PM
> > > User: N/A
> > > Computer: DATASERVERA
> > > Description:
> > > 17066 :
> > > SQL Server Assertion: File: <recbase.cpp>, line=1378
> > > Failed Assertion = 'm_offBeginVar < m_SizeRec'.
> > >
> > >
> > >
> > > -doug
> > >
> > >
> >
> >
>|||Thanks for all your help. I found some corrupt indexes(sp.) that I believe
were causing me some difficulties.....
-doug
"Paul S Randal [MS]" <prandal@.online.microsoft.com> wrote in message
news:eJSuSMTjDHA.1132@.tk2msftngp13.phx.gbl...
> Any disk, controller or IO subsystem diagnostics you can run. I don't know
> about the PID.
> There should also be some sqlXXXXX.dmp files in the same directory as the
> errorlog which will be useful to PSS as they will have the call stacks at
> the time the assert fires.
> Regards,
> Paul.
> --
> Paul Randal
> DBCC Technical Lead, Microsoft SQL Server Storage Engine
> This posting is provided "AS IS" with no warranties, and confers no
rights.
> "Doug Threewitt" <doug@.seisystems.com> wrote in message
> news:#EAOl7QjDHA.2160@.TK2MSFTNGP10.phx.gbl...
> > I've run DBCC CHECKDB on the database and it found no corruption. The
> event
> > log shows this error occuring a few other times. What hardware
> diagnostics
> > do you recommend running? Also, how can I find my PID for the SQL
Server
> so
> > I can get some help from Microsoft regarding this matter. Thanks for
all
> > your help
> >
> >
> > -doug
> >
> >
> > "Paul S Randal [MS]" <prandal@.online.microsoft.com> wrote in message
> > news:%23gO4Z2OjDHA.2160@.TK2MSFTNGP12.phx.gbl...
> > > This usually points to hardware-caused corruption in your database.
The
> > > assert is basically saying that a record on a page is corrupt. You can
> > > contact Product Support (http://support.microsoft.com) who will be
able
> to
> > > help or do the following:
> > > 1) run DBCC CHECKDB on the database to determine if any corruption is
> > > present
> > > 2) check the NT event log and SQL Server errorlog for previous
> > > errors/problems
> > > 3) run hardware diagnostics
> > > 4) if corruption is present, restore the database from your up-to-date
> > > backups onto different hardware
> > >
> > > Regards,
> > >
> > > Paul.
> > >
> > > --
> > > Paul Randal
> > > DBCC Technical Lead, Microsoft SQL Server Storage Engine
> > >
> > > This posting is provided "AS IS" with no warranties, and confers no
> > rights.
> > > "Doug Threewitt" <doug@.seisystems.com> wrote in message
> > > news:#lK4EhDjDHA.488@.TK2MSFTNGP09.phx.gbl...
> > > > Can anyone help me with this error? The query that is causing
> this
> > > > problem was running like a champ up until about 4 days ago. I have
> done
> > > no
> > > > upgrades to the database server in the last couple weeks. Thanks.
> > > >
> > > > Event Type: Error
> > > > Event Source: MSSQLSERVER
> > > > Event Category: (2)
> > > > Event ID: 17055
> > > > Date: 10/6/2003
> > > > Time: 12:08:48 PM
> > > > User: N/A
> > > > Computer: DATASERVERA
> > > > Description:
> > > > 17066 :
> > > > SQL Server Assertion: File: <recbase.cpp>, line=1378
> > > > Failed Assertion = 'm_offBeginVar < m_SizeRec'.
> > > >
> > > >
> > > >
> > > > -doug
> > > >
> > > >
> > >
> > >
> >
> >
>

Tuesday, March 20, 2012

Rebuild Index helps temporarily

I have a query that grinds to a halt after a heavy load. I have
discovered, through many variations and trials, that if I rebuild the
index of a certain table, my performance improves incredibly. Once the
load increases, the performance degrades. We rebuild our indexes daily
(overkill, but we are not 24 x 7). If you look at the table's indexes
the fragmentation is about 30%. It does NOT change after a rebuild
even though the performance does. Any ideas? (It is not a huge table
~ 5000 rows. Medium on inserts and updates)
These are the results of the SHOWCONTIG sproc
DBCC SHOWCONTIG scanning 'InventoryCount' table...
Table: 'InventoryCount' (747149707); index ID: 1, database ID: 11
TABLE level scan performed.
- Pages Scanned........................: 28
- Extents Scanned.......................: 10
- Extent Switches.......................: 9
- Avg. Pages per Extent..................: 2.8
- Scan Density [Best Count:Actual Count]......: 40.00% [4:10]
- Logical Scan Fragmentation ..............: 25.00%
- Extent Scan Fragmentation ...............: 70.00%
- Avg. Bytes Free per Page................: 2499.0
- Avg. Page Density (full)................: 69.13%
What is the fill factor of the rebuild command you are using and how does the
query looks like?
"magkip@.hotmail.com" wrote:

> I have a query that grinds to a halt after a heavy load. I have
> discovered, through many variations and trials, that if I rebuild the
> index of a certain table, my performance improves incredibly. Once the
> load increases, the performance degrades. We rebuild our indexes daily
> (overkill, but we are not 24 x 7). If you look at the table's indexes
> the fragmentation is about 30%. It does NOT change after a rebuild
> even though the performance does. Any ideas? (It is not a huge table
> ~ 5000 rows. Medium on inserts and updates)
> These are the results of the SHOWCONTIG sproc
> DBCC SHOWCONTIG scanning 'InventoryCount' table...
> Table: 'InventoryCount' (747149707); index ID: 1, database ID: 11
> TABLE level scan performed.
> - Pages Scanned........................: 28
> - Extents Scanned.......................: 10
> - Extent Switches.......................: 9
> - Avg. Pages per Extent..................: 2.8
> - Scan Density [Best Count:Actual Count]......: 40.00% [4:10]
> - Logical Scan Fragmentation ..............: 25.00%
> - Extent Scan Fragmentation ...............: 70.00%
> - Avg. Bytes Free per Page................: 2499.0
> - Avg. Page Density (full)................: 69.13%
>
|||The query is long and complicated. We are optimizing it now. It can
run in <2 seconds after the rebuild.
I set the fill factor to 80%.
Edgardo wrote:[vbcol=seagreen]
> What is the fill factor of the rebuild command you are using and how does the
> query looks like?
> "magkip@.hotmail.com" wrote:
|||Oops. I misunderstood. I just use 'Rebuild All indexes'.
mag...@.hotmail.com wrote:[vbcol=seagreen]
> The query is long and complicated. We are optimizing it now. It can
> run in <2 seconds after the rebuild.
> I set the fill factor to 80%.
> Edgardo wrote:
|||Edgardo and Magkip,
I found this interesting and have tried to increase the fill factor on some
tables in the past. In my case however, I boosted it only slightly (from 10
percent to 15 percent). Magkip, you have boosted it to 80 percent.
Is there a method to determine just how much to change this fill amount?
For example, would a change to 80 percent mean that the table will grow by as
much as 80 percent of the original size each time it grows or is this more of
a way of forcing the disk allocation to be large enough to hold any temporary
space required during the growth? Or is it some of both?
Regards,
Jamie
"Edgardo Valdez, MCTS / MCITP" wrote:
[vbcol=seagreen]
> What is the fill factor of the rebuild command you are using and how does the
> query looks like?
> "magkip@.hotmail.com" wrote:

Rebuild Index helps temporarily

I have a query that grinds to a halt after a heavy load. I have
discovered, through many variations and trials, that if I rebuild the
index of a certain table, my performance improves incredibly. Once the
load increases, the performance degrades. We rebuild our indexes daily
(overkill, but we are not 24 x 7). If you look at the table's indexes
the fragmentation is about 30%. It does NOT change after a rebuild
even though the performance does. Any ideas? (It is not a huge table
~ 5000 rows. Medium on inserts and updates)
These are the results of the SHOWCONTIG sproc
DBCC SHOWCONTIG scanning 'InventoryCount' table...
Table: 'InventoryCount' (747149707); index ID: 1, database ID: 11
TABLE level scan performed.
- Pages Scanned........................: 28
- Extents Scanned.......................: 10
- Extent Switches.......................: 9
- Avg. Pages per Extent..................: 2.8
- Scan Density [Best Count:Actual Count]......: 40.00% [4:10]
- Logical Scan Fragmentation ..............: 25.00%
- Extent Scan Fragmentation ...............: 70.00%
- Avg. Bytes Free per Page................: 2499.0
- Avg. Page Density (full)................: 69.13%What is the fill factor of the rebuild command you are using and how does the
query looks like?
"magkip@.hotmail.com" wrote:
> I have a query that grinds to a halt after a heavy load. I have
> discovered, through many variations and trials, that if I rebuild the
> index of a certain table, my performance improves incredibly. Once the
> load increases, the performance degrades. We rebuild our indexes daily
> (overkill, but we are not 24 x 7). If you look at the table's indexes
> the fragmentation is about 30%. It does NOT change after a rebuild
> even though the performance does. Any ideas? (It is not a huge table
> ~ 5000 rows. Medium on inserts and updates)
> These are the results of the SHOWCONTIG sproc
> DBCC SHOWCONTIG scanning 'InventoryCount' table...
> Table: 'InventoryCount' (747149707); index ID: 1, database ID: 11
> TABLE level scan performed.
> - Pages Scanned........................: 28
> - Extents Scanned.......................: 10
> - Extent Switches.......................: 9
> - Avg. Pages per Extent..................: 2.8
> - Scan Density [Best Count:Actual Count]......: 40.00% [4:10]
> - Logical Scan Fragmentation ..............: 25.00%
> - Extent Scan Fragmentation ...............: 70.00%
> - Avg. Bytes Free per Page................: 2499.0
> - Avg. Page Density (full)................: 69.13%
>|||The query is long and complicated. We are optimizing it now. It can
run in <2 seconds after the rebuild.
I set the fill factor to 80%.
Edgardo wrote:
> What is the fill factor of the rebuild command you are using and how does the
> query looks like?
> "magkip@.hotmail.com" wrote:
> > I have a query that grinds to a halt after a heavy load. I have
> > discovered, through many variations and trials, that if I rebuild the
> > index of a certain table, my performance improves incredibly. Once the
> > load increases, the performance degrades. We rebuild our indexes daily
> > (overkill, but we are not 24 x 7). If you look at the table's indexes
> > the fragmentation is about 30%. It does NOT change after a rebuild
> > even though the performance does. Any ideas? (It is not a huge table
> > ~ 5000 rows. Medium on inserts and updates)
> >
> > These are the results of the SHOWCONTIG sproc
> > DBCC SHOWCONTIG scanning 'InventoryCount' table...
> > Table: 'InventoryCount' (747149707); index ID: 1, database ID: 11
> > TABLE level scan performed.
> > - Pages Scanned........................: 28
> > - Extents Scanned.......................: 10
> > - Extent Switches.......................: 9
> > - Avg. Pages per Extent..................: 2.8
> > - Scan Density [Best Count:Actual Count]......: 40.00% [4:10]
> > - Logical Scan Fragmentation ..............: 25.00%
> > - Extent Scan Fragmentation ...............: 70.00%
> > - Avg. Bytes Free per Page................: 2499.0
> > - Avg. Page Density (full)................: 69.13%
> >
> >|||Oops. I misunderstood. I just use 'Rebuild All indexes'.
mag...@.hotmail.com wrote:
> The query is long and complicated. We are optimizing it now. It can
> run in <2 seconds after the rebuild.
> I set the fill factor to 80%.
> Edgardo wrote:
> > What is the fill factor of the rebuild command you are using and how does the
> > query looks like?
> >
> > "magkip@.hotmail.com" wrote:
> >
> > > I have a query that grinds to a halt after a heavy load. I have
> > > discovered, through many variations and trials, that if I rebuild the
> > > index of a certain table, my performance improves incredibly. Once the
> > > load increases, the performance degrades. We rebuild our indexes daily
> > > (overkill, but we are not 24 x 7). If you look at the table's indexes
> > > the fragmentation is about 30%. It does NOT change after a rebuild
> > > even though the performance does. Any ideas? (It is not a huge table
> > > ~ 5000 rows. Medium on inserts and updates)
> > >
> > > These are the results of the SHOWCONTIG sproc
> > > DBCC SHOWCONTIG scanning 'InventoryCount' table...
> > > Table: 'InventoryCount' (747149707); index ID: 1, database ID: 11
> > > TABLE level scan performed.
> > > - Pages Scanned........................: 28
> > > - Extents Scanned.......................: 10
> > > - Extent Switches.......................: 9
> > > - Avg. Pages per Extent..................: 2.8
> > > - Scan Density [Best Count:Actual Count]......: 40.00% [4:10]
> > > - Logical Scan Fragmentation ..............: 25.00%
> > > - Extent Scan Fragmentation ...............: 70.00%
> > > - Avg. Bytes Free per Page................: 2499.0
> > > - Avg. Page Density (full)................: 69.13%
> > >
> > >|||Edgardo and Magkip,
I found this interesting and have tried to increase the fill factor on some
tables in the past. In my case however, I boosted it only slightly (from 10
percent to 15 percent). Magkip, you have boosted it to 80 percent.
Is there a method to determine just how much to change this fill amount?
For example, would a change to 80 percent mean that the table will grow by as
much as 80 percent of the original size each time it grows or is this more of
a way of forcing the disk allocation to be large enough to hold any temporary
space required during the growth? Or is it some of both?
--
Regards,
Jamie
"Edgardo Valdez, MCTS / MCITP" wrote:
> What is the fill factor of the rebuild command you are using and how does the
> query looks like?
> "magkip@.hotmail.com" wrote:
> > I have a query that grinds to a halt after a heavy load. I have
> > discovered, through many variations and trials, that if I rebuild the
> > index of a certain table, my performance improves incredibly. Once the
> > load increases, the performance degrades. We rebuild our indexes daily
> > (overkill, but we are not 24 x 7). If you look at the table's indexes
> > the fragmentation is about 30%. It does NOT change after a rebuild
> > even though the performance does. Any ideas? (It is not a huge table
> > ~ 5000 rows. Medium on inserts and updates)
> >
> > These are the results of the SHOWCONTIG sproc
> > DBCC SHOWCONTIG scanning 'InventoryCount' table...
> > Table: 'InventoryCount' (747149707); index ID: 1, database ID: 11
> > TABLE level scan performed.
> > - Pages Scanned........................: 28
> > - Extents Scanned.......................: 10
> > - Extent Switches.......................: 9
> > - Avg. Pages per Extent..................: 2.8
> > - Scan Density [Best Count:Actual Count]......: 40.00% [4:10]
> > - Logical Scan Fragmentation ..............: 25.00%
> > - Extent Scan Fragmentation ...............: 70.00%
> > - Avg. Bytes Free per Page................: 2499.0
> > - Avg. Page Density (full)................: 69.13%
> >
> >

Rebuild Index helps temporarily

I have a query that grinds to a halt after a heavy load. I have
discovered, through many variations and trials, that if I rebuild the
index of a certain table, my performance improves incredibly. Once the
load increases, the performance degrades. We rebuild our indexes daily
(overkill, but we are not 24 x 7). If you look at the table's indexes
the fragmentation is about 30%. It does NOT change after a rebuild
even though the performance does. Any ideas? (It is not a huge table
~ 5000 rows. Medium on inserts and updates)
These are the results of the SHOWCONTIG sproc
DBCC SHOWCONTIG scanning 'InventoryCount' table...
Table: 'InventoryCount' (747149707); index ID: 1, database ID: 11
TABLE level scan performed.
- Pages Scanned........................: 28
- Extents Scanned.......................: 10
- Extent Switches.......................: 9
- Avg. Pages per Extent..................: 2.8
- Scan Density [Best Count:Actual Count]......: 40.00% [4:10]
- Logical Scan Fragmentation ..............: 25.00%
- Extent Scan Fragmentation ...............: 70.00%
- Avg. Bytes Free per Page................: 2499.0
- Avg. Page Density (full)................: 69.13%What is the fill factor of the rebuild command you are using and how does th
e
query looks like?
"magkip@.hotmail.com" wrote:

> I have a query that grinds to a halt after a heavy load. I have
> discovered, through many variations and trials, that if I rebuild the
> index of a certain table, my performance improves incredibly. Once the
> load increases, the performance degrades. We rebuild our indexes daily
> (overkill, but we are not 24 x 7). If you look at the table's indexes
> the fragmentation is about 30%. It does NOT change after a rebuild
> even though the performance does. Any ideas? (It is not a huge table
> ~ 5000 rows. Medium on inserts and updates)
> These are the results of the SHOWCONTIG sproc
> DBCC SHOWCONTIG scanning 'InventoryCount' table...
> Table: 'InventoryCount' (747149707); index ID: 1, database ID: 11
> TABLE level scan performed.
> - Pages Scanned........................: 28
> - Extents Scanned.......................: 10
> - Extent Switches.......................: 9
> - Avg. Pages per Extent..................: 2.8
> - Scan Density [Best Count:Actual Count]......: 40.00% [4:10]
> - Logical Scan Fragmentation ..............: 25.00%
> - Extent Scan Fragmentation ...............: 70.00%
> - Avg. Bytes Free per Page................: 2499.0
> - Avg. Page Density (full)................: 69.13%
>|||The query is long and complicated. We are optimizing it now. It can
run in <2 seconds after the rebuild.
I set the fill factor to 80%.
Edgardo wrote:[vbcol=seagreen]
> What is the fill factor of the rebuild command you are using and how does
the
> query looks like?
> "magkip@.hotmail.com" wrote:
>|||Oops. I misunderstood. I just use 'Rebuild All indexes'.
mag...@.hotmail.com wrote:[vbcol=seagreen]
> The query is long and complicated. We are optimizing it now. It can
> run in <2 seconds after the rebuild.
> I set the fill factor to 80%.
> Edgardo wrote:|||Edgardo and Magkip,
I found this interesting and have tried to increase the fill factor on some
tables in the past. In my case however, I boosted it only slightly (from 10
percent to 15 percent). Magkip, you have boosted it to 80 percent.
Is there a method to determine just how much to change this fill amount?
For example, would a change to 80 percent mean that the table will grow by a
s
much as 80 percent of the original size each time it grows or is this more o
f
a way of forcing the disk allocation to be large enough to hold any temporar
y
space required during the growth? Or is it some of both?
--
Regards,
Jamie
"Edgardo Valdez, MCTS / MCITP" wrote:
[vbcol=seagreen]
> What is the fill factor of the rebuild command you are using and how does
the
> query looks like?
> "magkip@.hotmail.com" wrote:
>

Friday, March 9, 2012

Reasons for query execution time difference ??

Hi,
The problem faced by me is:
I tried to do a performance test on our product using Load Runner.
We captured the scripts and executed them for single user, 1 iteration.
Time taken by the entire transaction to complete at various attempts:
Attempt 1: 700 ms
Attempt 2: 17 seconds
Attempt 3: 3 seconds
Attempt 4: 18 seconds
Attempt 5: 3 seconds
Note: Before every attempt, I restored the database from a backup and
executed "DBCC DBREINDEX" and "UPDATE STATISTICS" to ensure that all the
indexes & statistics are up to date...
There is no difference in the Server anbd Client settings, environments and
the network settings...
Due to this inconsistent behaviour, I could not arrive at any kind of
conclusion about which query to tune/ which is creating the performance
bottle neck...
Interestingly, there are some queries, which always takes more time in all
the attempts and the response time for those queries are propotional to the
total time taken by the entire transaction... i.e. Query 1 takes 93 ms in
Attempt 1 and 9 sec in Attempt 2 and so on...
Moreover, whenever the system response is very poor (Attempt 2 & 4) I could
see queries related to statistics
(SELECT statman([order_id],[order_no],@.PSTATMAN) FROM (SELECT TOP
100 PERCENT [order_id],[order_no] FROM [dbo].[ord_services]
WITH(READUNCOMMITTED,SAMPLE 1.863186e+000 PERCENT) ORDER BY
[order_id],[order_no]) AS _MS_UPDSTATS_TBL OPTION (BYPASS OPTIMIZER_QUEUE,
MAXDOP 1)
which are fired internally by the Query Optimizer to decide on the query
plan and these queries seems to take 50% of the transaction time...
Can any one tell me about the possible reasons for the difference in the SQL
Server behaviour and the ways to solve this...
I am using SQL Server 2000 Service Pack 4
Balasubramaniam. M
Associate Consultant
SIEMENS Information Systems Ltd.
Bangalore
India
These queries look like it is updating statistics. Is Auto update statistics
turned on?... If so, try turning it off and repeating the test to see if you
get more uniform results.
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"Bala" <Bala@.discussions.microsoft.com> wrote in message
news:A7CFC5E3-C966-44B0-904D-121045F86F04@.microsoft.com...
> Hi,
> The problem faced by me is:
> I tried to do a performance test on our product using Load Runner.
> We captured the scripts and executed them for single user, 1 iteration.
> Time taken by the entire transaction to complete at various attempts:
> Attempt 1: 700 ms
> Attempt 2: 17 seconds
> Attempt 3: 3 seconds
> Attempt 4: 18 seconds
> Attempt 5: 3 seconds
> Note: Before every attempt, I restored the database from a backup and
> executed "DBCC DBREINDEX" and "UPDATE STATISTICS" to ensure that all the
> indexes & statistics are up to date...
> There is no difference in the Server anbd Client settings, environments
and
> the network settings...
> Due to this inconsistent behaviour, I could not arrive at any kind of
> conclusion about which query to tune/ which is creating the performance
> bottle neck...
> Interestingly, there are some queries, which always takes more time in all
> the attempts and the response time for those queries are propotional to
the
> total time taken by the entire transaction... i.e. Query 1 takes 93 ms in
> Attempt 1 and 9 sec in Attempt 2 and so on...
> Moreover, whenever the system response is very poor (Attempt 2 & 4) I
could
> see queries related to statistics
> (SELECT statman([order_id],[order_no],@.PSTATMAN) FROM (SELECT TOP
> 100 PERCENT [order_id],[order_no] FROM [dbo].[ord_services]
> WITH(READUNCOMMITTED,SAMPLE 1.863186e+000 PERCENT) ORDER BY
> [order_id],[order_no]) AS _MS_UPDSTATS_TBL OPTION (BYPASS OPTIMIZER_QUEUE,
> MAXDOP 1)
> which are fired internally by the Query Optimizer to decide on the query
> plan and these queries seems to take 50% of the transaction time...
> Can any one tell me about the possible reasons for the difference in the
SQL
> Server behaviour and the ways to solve this...
> I am using SQL Server 2000 Service Pack 4
> --
> Balasubramaniam. M
> Associate Consultant
> SIEMENS Information Systems Ltd.
> Bangalore
> India
>

Reasons for query execution time difference ??

Hi,
The problem faced by me is:
I tried to do a performance test on our product using Load Runner.
We captured the scripts and executed them for single user, 1 iteration.
Time taken by the entire transaction to complete at various attempts:
Attempt 1: 700 ms
Attempt 2: 17 seconds
Attempt 3: 3 seconds
Attempt 4: 18 seconds
Attempt 5: 3 seconds
Note: Before every attempt, I restored the database from a backup and
executed "DBCC DBREINDEX" and "UPDATE STATISTICS" to ensure that all the
indexes & statistics are up to date...
There is no difference in the Server anbd Client settings, environments and
the network settings...
Due to this inconsistent behaviour, I could not arrive at any kind of
conclusion about which query to tune/ which is creating the performance
bottle neck...
Interestingly, there are some queries, which always takes more time in all
the attempts and the response time for those queries are propotional to the
total time taken by the entire transaction... i.e. Query 1 takes 93 ms in
Attempt 1 and 9 sec in Attempt 2 and so on...
Moreover, whenever the system response is very poor (Attempt 2 & 4) I could
see queries related to statistics
(SELECT statman([order_id],[order_no],@.PSTATMAN) FROM (SELECT TOP
100 PERCENT [order_id],[order_no] FROM [dbo].[ord_services]
WITH(READUNCOMMITTED,SAMPLE 1.863186e+000 PERCENT) ORDER BY
[order_id],[order_no]) AS _MS_UPDSTATS_TBL OPTION (BYPASS OPTIMIZER_QUEUE,
MAXDOP 1)
which are fired internally by the Query Optimizer to decide on the query
plan and these queries seems to take 50% of the transaction time...
Can any one tell me about the possible reasons for the difference in the SQL
Server behaviour and the ways to solve this...
I am using SQL Server 2000 Service Pack 4
--
Balasubramaniam. M
Associate Consultant
SIEMENS Information Systems Ltd.
Bangalore
IndiaThese queries look like it is updating statistics. Is Auto update statistics
turned on?... If so, try turning it off and repeating the test to see if you
get more uniform results.
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"Bala" <Bala@.discussions.microsoft.com> wrote in message
news:A7CFC5E3-C966-44B0-904D-121045F86F04@.microsoft.com...
> Hi,
> The problem faced by me is:
> I tried to do a performance test on our product using Load Runner.
> We captured the scripts and executed them for single user, 1 iteration.
> Time taken by the entire transaction to complete at various attempts:
> Attempt 1: 700 ms
> Attempt 2: 17 seconds
> Attempt 3: 3 seconds
> Attempt 4: 18 seconds
> Attempt 5: 3 seconds
> Note: Before every attempt, I restored the database from a backup and
> executed "DBCC DBREINDEX" and "UPDATE STATISTICS" to ensure that all the
> indexes & statistics are up to date...
> There is no difference in the Server anbd Client settings, environments
and
> the network settings...
> Due to this inconsistent behaviour, I could not arrive at any kind of
> conclusion about which query to tune/ which is creating the performance
> bottle neck...
> Interestingly, there are some queries, which always takes more time in all
> the attempts and the response time for those queries are propotional to
the
> total time taken by the entire transaction... i.e. Query 1 takes 93 ms in
> Attempt 1 and 9 sec in Attempt 2 and so on...
> Moreover, whenever the system response is very poor (Attempt 2 & 4) I
could
> see queries related to statistics
> (SELECT statman([order_id],[order_no],@.PSTATMAN) FROM (SELECT TOP
> 100 PERCENT [order_id],[order_no] FROM [dbo].[ord_services]
> WITH(READUNCOMMITTED,SAMPLE 1.863186e+000 PERCENT) ORDER BY
> [order_id],[order_no]) AS _MS_UPDSTATS_TBL OPTION (BYPASS OPTIMIZER_QUEUE,
> MAXDOP 1)
> which are fired internally by the Query Optimizer to decide on the query
> plan and these queries seems to take 50% of the transaction time...
> Can any one tell me about the possible reasons for the difference in the
SQL
> Server behaviour and the ways to solve this...
> I am using SQL Server 2000 Service Pack 4
> --
> Balasubramaniam. M
> Associate Consultant
> SIEMENS Information Systems Ltd.
> Bangalore
> India
>

Reasons for query execution time difference ??

Hi,
The problem faced by me is:
I tried to do a performance test on our product using Load Runner.
We captured the scripts and executed them for single user, 1 iteration.
Time taken by the entire transaction to complete at various attempts:
Attempt 1: 700 ms
Attempt 2: 17 seconds
Attempt 3: 3 seconds
Attempt 4: 18 seconds
Attempt 5: 3 seconds
Note: Before every attempt, I restored the database from a backup and
executed "DBCC DBREINDEX" and "UPDATE STATISTICS" to ensure that all the
indexes & statistics are up to date...
There is no difference in the Server anbd Client settings, environments and
the network settings...
Due to this inconsistent behaviour, I could not arrive at any kind of
conclusion about which query to tune/ which is creating the performance
bottle neck...
Interestingly, there are some queries, which always takes more time in all
the attempts and the response time for those queries are propotional to the
total time taken by the entire transaction... i.e. Query 1 takes 93 ms in
Attempt 1 and 9 sec in Attempt 2 and so on...
Moreover, whenever the system response is very poor (Attempt 2 & 4) I could
see queries related to statistics
(SELECT statman([order_id],[order_no],@.PSTATMAN) FROM (SELEC
T TOP
100 PERCENT [order_id],[order_no] FROM [dbo].[ord_services]
WITH(READUNCOMMITTED,SAMPLE 1.863186e+000 PERCENT) ORDER BY
[order_id],[order_no]) AS _MS_UPDSTATS_TBL OPTION (BYPASS OPTIMIZER_
QUEUE,
MAXDOP 1)
which are fired internally by the Query Optimizer to decide on the query
plan and these queries seems to take 50% of the transaction time...
Can any one tell me about the possible reasons for the difference in the SQL
Server behaviour and the ways to solve this...
I am using SQL Server 2000 Service Pack 4
Balasubramaniam. M
Associate Consultant
SIEMENS Information Systems Ltd.
Bangalore
IndiaThese queries look like it is updating statistics. Is Auto update statistics
turned on?... If so, try turning it off and repeating the test to see if you
get more uniform results.
Wayne Snyder, MCDBA, SQL Server MVP
Mariner, Charlotte, NC
www.mariner-usa.com
(Please respond only to the newsgroups.)
I support the Professional Association of SQL Server (PASS) and it's
community of SQL Server professionals.
www.sqlpass.org
"Bala" <Bala@.discussions.microsoft.com> wrote in message
news:A7CFC5E3-C966-44B0-904D-121045F86F04@.microsoft.com...
> Hi,
> The problem faced by me is:
> I tried to do a performance test on our product using Load Runner.
> We captured the scripts and executed them for single user, 1 iteration.
> Time taken by the entire transaction to complete at various attempts:
> Attempt 1: 700 ms
> Attempt 2: 17 seconds
> Attempt 3: 3 seconds
> Attempt 4: 18 seconds
> Attempt 5: 3 seconds
> Note: Before every attempt, I restored the database from a backup and
> executed "DBCC DBREINDEX" and "UPDATE STATISTICS" to ensure that all the
> indexes & statistics are up to date...
> There is no difference in the Server anbd Client settings, environments
and
> the network settings...
> Due to this inconsistent behaviour, I could not arrive at any kind of
> conclusion about which query to tune/ which is creating the performance
> bottle neck...
> Interestingly, there are some queries, which always takes more time in all
> the attempts and the response time for those queries are propotional to
the
> total time taken by the entire transaction... i.e. Query 1 takes 93 ms in
> Attempt 1 and 9 sec in Attempt 2 and so on...
> Moreover, whenever the system response is very poor (Attempt 2 & 4) I
could
> see queries related to statistics
> (SELECT statman([order_id],[order_no],@.PSTATMAN) FROM (SEL
ECT TOP
> 100 PERCENT [order_id],[order_no] FROM [dbo].[ord_services
]
> WITH(READUNCOMMITTED,SAMPLE 1.863186e+000 PERCENT) ORDER BY
> [order_id],[order_no]) AS _MS_UPDSTATS_TBL OPTION (BYPASS OPTIMIZE
R_QUEUE,
> MAXDOP 1)
> which are fired internally by the Query Optimizer to decide on the query
> plan and these queries seems to take 50% of the transaction time...
> Can any one tell me about the possible reasons for the difference in the
SQL
> Server behaviour and the ways to solve this...
> I am using SQL Server 2000 Service Pack 4
> --
> Balasubramaniam. M
> Associate Consultant
> SIEMENS Information Systems Ltd.
> Bangalore
> India
>

Reasonably simple query question

I'm an SQL beginner and this is driving me nuts as it seems simple enough
but I can't figure it out.

I have a table that looks like:

ID: int
MajorVersion: int
MinorVersion: int
Content: ntext

The ID is not the table key - different rows can have the same ID.

The MajorVersion and MinorVersion columns together make up a version number.
Say if a row represented version 1.8 of that ID, MajorVersion would be 1 and
MinorVersion 8.

All I want to do is query for ID and Content for the highest versions of
each ID. I tried using GROUP BY but I can't do that because I can't include
Content in the SELECT then.
Am I going to have to just query for the ID and then do a join to get the
Content?

Thanks for any help

Chris"Chris Vinall" <cvinall@.nospam.myrealbox.com> wrote in message
news:3f7643f5@.duster.adelaide.on.net...
> I'm an SQL beginner and this is driving me nuts as it seems simple enough
> but I can't figure it out.
> I have a table that looks like:
> ID: int
> MajorVersion: int
> MinorVersion: int
> Content: ntext
> The ID is not the table key - different rows can have the same ID.
> The MajorVersion and MinorVersion columns together make up a version number.
> Say if a row represented version 1.8 of that ID, MajorVersion would be 1 and
> MinorVersion 8.
> All I want to do is query for ID and Content for the highest versions of
> each ID. I tried using GROUP BY but I can't do that because I can't include
> Content in the SELECT then.
> Am I going to have to just query for the ID and then do a join to get the
> Content?
> Thanks for any help
> Chris

Assume table is T.

SELECT T1.ID, T1.Content
FROM T AS T1
LEFT OUTER JOIN
T AS T2
ON T1.ID = T2.ID AND
(T2.MajorVersion > T1.MajorVersion OR
(T2.MajorVersion = T1.MajorVersion AND
T2.MinorVersion > T1.MinorVersion))
WHERE T2.ID IS NULL

Regards,
jag|||Thanks muchly :)

I would never have thought of doing it like that

"John Gilson" <jag@.acm.org> wrote in message
news:eUrdb.53380$u67.35809@.twister.nyc.rr.com...
> Assume table is T.
> SELECT T1.ID, T1.Content
> FROM T AS T1
> LEFT OUTER JOIN
> T AS T2
> ON T1.ID = T2.ID AND
> (T2.MajorVersion > T1.MajorVersion OR
> (T2.MajorVersion = T1.MajorVersion AND
> T2.MinorVersion > T1.MinorVersion))
> WHERE T2.ID IS NULL
> Regards,
> jag|||>> I have a table that looks like: <<

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in
your schema are. Sample data is also a good ideas, along with clear
specifications. Is this what you meant, if you had followed the
newsgroup's netiquette?

CREATE TABLE Foobar
(product_id INTEGER NOT NULL,
major_version_nbr INTEGER NOT NULL,
minor_version_nbr INTEGER NOT NULL,
content NTEXT NOT NULL,
PRIMARY KEY (product_id, major_version_nbr, minor_version_nbr));

You also need to read ISO-11179 so that you will stop using vague,
meaningless data element names like "id" (of what??). Next, one of
the rules of data modeling is that you do not split an attribute over
multiple columns; a column is an attribute drawn from one and only one
domain and it represents a complete measurment or value in itself.
That is why you use a date and not three separate columns for year,
month and day.

Likewise, a row in a table is a complete fact, but that is another
topic. Let's fix your mess:

CREATE TABLE Foobar -- done right
(product_id INTEGER NOT NULL,
version_nbr DECIMAL (8,4) NOT NULL,
content NTEXT NOT NULL,
PRIMARY KEY (product_id, version_nbr));

>> All I want to do is query for ID and Content for the highest
versions of each ID. <<

SELECT F1.product_id, F1.content
FROM Foobar AS F1
WHERE version_nbr
= (SELECT MAX(version_nbr)
FROM Foobar AS F2
WHERE F1.product_id = F2.product_id);

Good rule of thumb: complex queries for simple things are most often
the result of poor schema design. Here is the same thing for your
schema.

SELECT F1.product_id, F1.content
FROM Foobar AS F1
WHERE F1.major_version_nbr
= (SELECT MAX(F2.major_version_nbr)
FROM Foobar AS F2
WHERE F1.product_id = F2.product_id)
AND F1.minor_version_nbr
= (SELECT MAX(F3.minor_version_nbr)
FROM Foobar AS F3
WHERE F1.product_id = F3.product_id
AND F3.major_version_nbr
= (SELECT MAX(F4.major_version_nbr)
FROM Foobar AS F4
WHERE F1.product_id = F2.product_id));|||joe.celko@.northface.edu (--CELKO--) wrote in message news:<a264e7ea.0309281016.7c0f6dcd@.posting.google.com>...
> >> I have a table that looks like: <<
> Please post DDL, so that people do not have to guess what the keys,
> constraints, Declarative Referential Integrity, datatypes, etc. in
> your schema are. Sample data is also a good ideas, along with clear
> specifications. Is this what you meant, if you had followed the
> newsgroup's netiquette?
> CREATE TABLE Foobar
> (product_id INTEGER NOT NULL,
> major_version_nbr INTEGER NOT NULL,
> minor_version_nbr INTEGER NOT NULL,
> content NTEXT NOT NULL,
> PRIMARY KEY (product_id, major_version_nbr, minor_version_nbr));
> You also need to read ISO-11179 so that you will stop using vague,
> meaningless data element names like "id" (of what??). Next, one of
> the rules of data modeling is that you do not split an attribute over
> multiple columns; a column is an attribute drawn from one and only one
> domain and it represents a complete measurment or value in itself.
> That is why you use a date and not three separate columns for year,
> month and day.
> Likewise, a row in a table is a complete fact, but that is another
> topic. Let's fix your mess:
> CREATE TABLE Foobar -- done right
> (product_id INTEGER NOT NULL,
> version_nbr DECIMAL (8,4) NOT NULL,
> content NTEXT NOT NULL,
> PRIMARY KEY (product_id, version_nbr));
> >> All I want to do is query for ID and Content for the highest
> versions of each ID. <<
> SELECT F1.product_id, F1.content
> FROM Foobar AS F1
> WHERE version_nbr
> = (SELECT MAX(version_nbr)
> FROM Foobar AS F2
> WHERE F1.product_id = F2.product_id);

and get the wrong result. Version specifications
are not numbers, so don't store them in a numeric
column. Major version 1, minor version 9 precedes
major version 1, minor version 11, but 1.9 does not
precede 1.11.

By the time version 1.10 rolls around and this query
breaks, it will be a lot of trouble to fix.

SK

> Good rule of thumb: complex queries for simple things are most often
> the result of poor schema design. Here is the same thing for your
> schema.
> SELECT F1.product_id, F1.content
> FROM Foobar AS F1
> WHERE F1.major_version_nbr
> = (SELECT MAX(F2.major_version_nbr)
> FROM Foobar AS F2
> WHERE F1.product_id = F2.product_id)
> AND F1.minor_version_nbr
> = (SELECT MAX(F3.minor_version_nbr)
> FROM Foobar AS F3
> WHERE F1.product_id = F3.product_id
> AND F3.major_version_nbr
> = (SELECT MAX(F4.major_version_nbr)
> FROM Foobar AS F4
> WHERE F1.product_id = F2.product_id));|||Joe,

>You also need to read ISO-11179 so that you will stop using vague...

Where can this be found?

Thanks in advance,
Christian.|||>> [ISO-11179] Where can this be found? <<

This is an international standard, not a trade secret; Google it. If you
do, you'll a few hundred hits, zipped files, powerpoints, etc.

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!|||"--CELKO--" <joe.celko@.northface.edu> wrote in message
news:a264e7ea.0309281016.7c0f6dcd@.posting.google.c om...
> Next, one of
> the rules of data modeling is that you do not split an attribute over
> multiple columns; a column is an attribute drawn from one and only one
> domain and it represents a complete measurment or value in itself.
> That is why you use a date and not three separate columns for year,
> month and day.

I build Cognos cubes for a living. Recently I was presented with a database
where they had managed to split a date into not three, but *FOUR* columns. A
2 digit year, a char(3) month (Jan, Feb, etc), an integer week and an
integer 'day of week'. I assumed I was looking at some complicated lookup
table so I asked where the actual dates were stored, and I was told "you're
looking at 'em".

ISO-11179 condensed into one word: "THINK!"

Regards Manning|||>> Version specifications are not numbers, so don't store them in a
numeric column. Major version 1, minor version 9 precedes major version
1, minor version 11, but 1.9 does not precede 1.11. <<

Which ISO standard is that? If you want to use lexical sorting, then
display the minor numbers with leading zeroes in the front end.

I like to use a "decimal outline" format when I write documents, which
*is* the ISO and ANSI convention -- look at the major sections of the
SQL-92 standard.

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!|||I was presented with a database where they had managed to split a date
into not three, but *FOUR* columns ... assumed I was looking at some
complicated lookup table so I asked where the actual dates were stored,
and I was told "you're looking at 'em". <<

I know it's not funny, but I gotta laugh. The one you see all over the
place is a series of identical tables for months or years that are
constantly being unioned back into a whole. Then someone who has
permissions on only the most recent memvber of the collection decides to
modifiy it ..

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!|||Joe Celko (joe.celko@.northface.edu) writes:
>>> Version specifications are not numbers, so don't store them in a
> numeric column. Major version 1, minor version 9 precedes major version
> 1, minor version 11, but 1.9 does not precede 1.11. <<
> Which ISO standard is that? If you want to use lexical sorting, then
> display the minor numbers with leading zeroes in the front end.

Not all business rules in this world is based on ISO standards, Joe.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Reasonable query never finishes

Hi everyone, I have a ~2GB database on SQL Server 2000 SP3 in which a certain
query works against some datasets within a couple tables, but mysteriously
never finishes against other datasets, perhaps some recently loaded ones.
It's mainly just one big table with some smaller auxiliary tables (used in
this query).
I've rebuilt the clustered indexes and run DBCC REINDEX and DBCC INDEXDEFRAG
without effect. However, if I do a DTS "Copy objects and data between SQL
Server databases" of the offending database to a fresh database, the problem
goes away (without touching indexes or anything).
As a further test, I restored a backup of the offending database onto a
different server, tried the query and it never returned, did the DTS copy
objects, queried again and it's fine. Past experience has shown that this
fresh, fixed database works fine for a while but can re-offend after a good
bit more data are loaded. It won't always be feasible to try this silly DTS
trick. The query plans for the query in the two databases are quite different
but I don't know enough to discern what the problem might be from comparing
them.
I've searched around but cannot see a similar issue, but maybe I haven't hit
upon the right search keywords.
Has anyone heard of or encountered such a problem? or other suggestions?
thanks,
Eric
Eric
Run sp_updatestatistics stored procedure on destination server.
(For more details please refer to the BOL)
"Eric Deutsch" <Eric Deutsch@.discussions.microsoft.com> wrote in message
news:62A9AF7B-DE5E-4B97-B593-00585488FF17@.microsoft.com...
> Hi everyone, I have a ~2GB database on SQL Server 2000 SP3 in which a
certain
> query works against some datasets within a couple tables, but mysteriously
> never finishes against other datasets, perhaps some recently loaded ones.
> It's mainly just one big table with some smaller auxiliary tables (used in
> this query).
> I've rebuilt the clustered indexes and run DBCC REINDEX and DBCC
INDEXDEFRAG
> without effect. However, if I do a DTS "Copy objects and data between SQL
> Server databases" of the offending database to a fresh database, the
problem
> goes away (without touching indexes or anything).
> As a further test, I restored a backup of the offending database onto a
> different server, tried the query and it never returned, did the DTS copy
> objects, queried again and it's fine. Past experience has shown that this
> fresh, fixed database works fine for a while but can re-offend after a
good
> bit more data are loaded. It won't always be feasible to try this silly
DTS
> trick. The query plans for the query in the two databases are quite
different
> but I don't know enough to discern what the problem might be from
comparing
> them.
> I've searched around but cannot see a similar issue, but maybe I haven't
hit
> upon the right search keywords.
> Has anyone heard of or encountered such a problem? or other suggestions?
> thanks,
> Eric
>
|||Hi Uri, many thanks for the reply. This indeed fixes the problem! Why is it
that I needed to do this when I have "auto update statistics" set to TRUE for
this database?
Is it a good idea to run sp_updatestats periodically regardless of the "auto
update statistics"?
Many thanks!
Eric
"Uri Dimant" wrote:

> Eric
> Run sp_updatestatistics stored procedure on destination server.
> (For more details please refer to the BOL)
>
>
> "Eric Deutsch" <Eric Deutsch@.discussions.microsoft.com> wrote in message
> news:62A9AF7B-DE5E-4B97-B593-00585488FF17@.microsoft.com...
> certain
> INDEXDEFRAG
> problem
> good
> DTS
> different
> comparing
> hit
>
>

Reasonable query never finishes

Hi everyone, I have a ~2GB database on SQL Server 2000 SP3 in which a certai
n
query works against some datasets within a couple tables, but mysteriously
never finishes against other datasets, perhaps some recently loaded ones.
It's mainly just one big table with some smaller auxiliary tables (used in
this query).
I've rebuilt the clustered indexes and run DBCC REINDEX and DBCC INDEXDEFRAG
without effect. However, if I do a DTS "Copy objects and data between SQL
Server databases" of the offending database to a fresh database, the problem
goes away (without touching indexes or anything).
As a further test, I restored a backup of the offending database onto a
different server, tried the query and it never returned, did the DTS copy
objects, queried again and it's fine. Past experience has shown that this
fresh, fixed database works fine for a while but can re-offend after a good
bit more data are loaded. It won't always be feasible to try this silly DTS
trick. The query plans for the query in the two databases are quite differen
t
but I don't know enough to discern what the problem might be from comparing
them.
I've searched around but cannot see a similar issue, but maybe I haven't hit
upon the right search keywords.
Has anyone heard of or encountered such a problem? or other suggestions?
thanks,
EricEric
Run sp_updatestatistics stored procedure on destination server.
(For more details please refer to the BOL)
"Eric Deutsch" <Eric Deutsch@.discussions.microsoft.com> wrote in message
news:62A9AF7B-DE5E-4B97-B593-00585488FF17@.microsoft.com...
> Hi everyone, I have a ~2GB database on SQL Server 2000 SP3 in which a
certain
> query works against some datasets within a couple tables, but mysteriously
> never finishes against other datasets, perhaps some recently loaded ones.
> It's mainly just one big table with some smaller auxiliary tables (used in
> this query).
> I've rebuilt the clustered indexes and run DBCC REINDEX and DBCC
INDEXDEFRAG
> without effect. However, if I do a DTS "Copy objects and data between SQL
> Server databases" of the offending database to a fresh database, the
problem
> goes away (without touching indexes or anything).
> As a further test, I restored a backup of the offending database onto a
> different server, tried the query and it never returned, did the DTS copy
> objects, queried again and it's fine. Past experience has shown that this
> fresh, fixed database works fine for a while but can re-offend after a
good
> bit more data are loaded. It won't always be feasible to try this silly
DTS
> trick. The query plans for the query in the two databases are quite
different
> but I don't know enough to discern what the problem might be from
comparing
> them.
> I've searched around but cannot see a similar issue, but maybe I haven't
hit
> upon the right search keywords.
> Has anyone heard of or encountered such a problem? or other suggestions?
> thanks,
> Eric
>|||Hi Uri, many thanks for the reply. This indeed fixes the problem! Why is it
that I needed to do this when I have "auto update statistics" set to TRUE fo
r
this database?
Is it a good idea to run sp_updatestats periodically regardless of the "auto
update statistics"?
Many thanks!
Eric
"Uri Dimant" wrote:

> Eric
> Run sp_updatestatistics stored procedure on destination server.
> (For more details please refer to the BOL)
>
>
> "Eric Deutsch" <Eric Deutsch@.discussions.microsoft.com> wrote in message
> news:62A9AF7B-DE5E-4B97-B593-00585488FF17@.microsoft.com...
> certain
> INDEXDEFRAG
> problem
> good
> DTS
> different
> comparing
> hit
>
>

Reasonable query never finishes

Hi everyone, I have a ~2GB database on SQL Server 2000 SP3 in which a certain
query works against some datasets within a couple tables, but mysteriously
never finishes against other datasets, perhaps some recently loaded ones.
It's mainly just one big table with some smaller auxiliary tables (used in
this query).
I've rebuilt the clustered indexes and run DBCC REINDEX and DBCC INDEXDEFRAG
without effect. However, if I do a DTS "Copy objects and data between SQL
Server databases" of the offending database to a fresh database, the problem
goes away (without touching indexes or anything).
As a further test, I restored a backup of the offending database onto a
different server, tried the query and it never returned, did the DTS copy
objects, queried again and it's fine. Past experience has shown that this
fresh, fixed database works fine for a while but can re-offend after a good
bit more data are loaded. It won't always be feasible to try this silly DTS
trick. The query plans for the query in the two databases are quite different
but I don't know enough to discern what the problem might be from comparing
them.
I've searched around but cannot see a similar issue, but maybe I haven't hit
upon the right search keywords.
Has anyone heard of or encountered such a problem? or other suggestions?
thanks,
EricEric
Run sp_updatestatistics stored procedure on destination server.
(For more details please refer to the BOL)
"Eric Deutsch" <Eric Deutsch@.discussions.microsoft.com> wrote in message
news:62A9AF7B-DE5E-4B97-B593-00585488FF17@.microsoft.com...
> Hi everyone, I have a ~2GB database on SQL Server 2000 SP3 in which a
certain
> query works against some datasets within a couple tables, but mysteriously
> never finishes against other datasets, perhaps some recently loaded ones.
> It's mainly just one big table with some smaller auxiliary tables (used in
> this query).
> I've rebuilt the clustered indexes and run DBCC REINDEX and DBCC
INDEXDEFRAG
> without effect. However, if I do a DTS "Copy objects and data between SQL
> Server databases" of the offending database to a fresh database, the
problem
> goes away (without touching indexes or anything).
> As a further test, I restored a backup of the offending database onto a
> different server, tried the query and it never returned, did the DTS copy
> objects, queried again and it's fine. Past experience has shown that this
> fresh, fixed database works fine for a while but can re-offend after a
good
> bit more data are loaded. It won't always be feasible to try this silly
DTS
> trick. The query plans for the query in the two databases are quite
different
> but I don't know enough to discern what the problem might be from
comparing
> them.
> I've searched around but cannot see a similar issue, but maybe I haven't
hit
> upon the right search keywords.
> Has anyone heard of or encountered such a problem? or other suggestions?
> thanks,
> Eric
>|||Hi Uri, many thanks for the reply. This indeed fixes the problem! Why is it
that I needed to do this when I have "auto update statistics" set to TRUE for
this database?
Is it a good idea to run sp_updatestats periodically regardless of the "auto
update statistics"?
Many thanks!
Eric
"Uri Dimant" wrote:
> Eric
> Run sp_updatestatistics stored procedure on destination server.
> (For more details please refer to the BOL)
>
>
> "Eric Deutsch" <Eric Deutsch@.discussions.microsoft.com> wrote in message
> news:62A9AF7B-DE5E-4B97-B593-00585488FF17@.microsoft.com...
> > Hi everyone, I have a ~2GB database on SQL Server 2000 SP3 in which a
> certain
> > query works against some datasets within a couple tables, but mysteriously
> > never finishes against other datasets, perhaps some recently loaded ones.
> > It's mainly just one big table with some smaller auxiliary tables (used in
> > this query).
> >
> > I've rebuilt the clustered indexes and run DBCC REINDEX and DBCC
> INDEXDEFRAG
> > without effect. However, if I do a DTS "Copy objects and data between SQL
> > Server databases" of the offending database to a fresh database, the
> problem
> > goes away (without touching indexes or anything).
> >
> > As a further test, I restored a backup of the offending database onto a
> > different server, tried the query and it never returned, did the DTS copy
> > objects, queried again and it's fine. Past experience has shown that this
> > fresh, fixed database works fine for a while but can re-offend after a
> good
> > bit more data are loaded. It won't always be feasible to try this silly
> DTS
> > trick. The query plans for the query in the two databases are quite
> different
> > but I don't know enough to discern what the problem might be from
> comparing
> > them.
> >
> > I've searched around but cannot see a similar issue, but maybe I haven't
> hit
> > upon the right search keywords.
> >
> > Has anyone heard of or encountered such a problem? or other suggestions?
> >
> > thanks,
> > Eric
> >
>
>

Wednesday, March 7, 2012

Rearrange String

I have a database where dates are stored in the following format yyyymmdd. I want to write a query where this is broken up and returned in the format mm-dd-yyyy ... Is this possible in SQL server?try this

SELECT CONVERT (datetime,, <DateFIELD> , 110)
FROM <TABLE>

Or

SELECT SUBSTR(<DateFIELD>, 5, 2) + "-" + SUBSTR(<DateFIELD>, 7, 2) + "-" + SUBSTR(<DateFIELD>, 1, 4) AS YOurField
FROM <TABLE>

<DateFIELD> - The name of your date field|||if you want mm-dd-yyyy then

select convert(varchar,getdate(),110)
select substring('20030201',5,2) + '-' + right('20030201',2) + '-' + left('20030201',4)

substitute your db field for getdate() or '20030227'|||Pardon my ignorance, but just for future reference, what is the "110" about?|||CONVERT (data_type[(length)], expression [, style])

it is the date style

0 or 100 (*) Default mon dd yyyy hh:miAM (or PM)
1 or 101 USA mm/dd/yy
2 or 102 ANSI yy.mm.dd
3 or 103 British/French dd/mm/yy
4 or 104 German dd.mm.yy
5 or 105 Italian dd-mm-yy
6 or 106 - dd mon yy
7 or 107 - mon dd, yy
8 or 108 - hh:mm:ss
9 or 109 (*) Default + milliseconds mon dd yyyy hh:mi:ss:mmmAM (or PM)
10 or 110 USA mm-dd-yy
11 or 111 JAPAN yy/mm/dd
12 or 112 ISO yymmdd
-13 or 113 (*) Europe default + milliseconds dd mon yyyy hh:mm:ss:mmm(24h)
14 or 114 - hh:mi:ss:mmm(24h)
20 or 120 (*) ODBC canonical yyyy-mm-dd hh:mi:ss(24h)
21 or 121 (*) ODBC canonical (with milliseconds) yyyy-mm-dd hh:mi:ss.mmm(24h)|||Look in bol under convert - it gives all the styles.
They affect converting to and from character format.|||Thanks guys... thats been really helpful... I have one other similar problem that ye might be able to help me with... In the same table I have a field for time and it's stored in the following format:

- 9:20am is stored as 920
- 3:30pm is stored as 1530
- 12:20am is stored as 20
- 12:05 is stored as 5

Do ye have any suggestion as to how I could call this back in the correct format?|||try:

if object_id('tempdb..#Tmp') is not null drop table #Tmp
create table #tmp(dt1 int, tm1 int,dt2 varchar(8), tm2 varchar(4))
insert into #tmp values(20030228,920,'20030228','920')
insert into #tmp values(20030228,1530,'20030228','1530')
insert into #tmp values(20030228,20,'20030228','20')
insert into #tmp values(20030228,5,'20030228','5')

select * From #tmp

select convert(varchar,cast(cast(dt1 as varchar) as datetime),110)
, substring(dt2,5,2) + '-' + right(dt2,2) + '-' + left(dt2,4)
from #tmp

select right(convert(varchar,cast('01-Jan-1753 ' +
left(right('0000' + cast(tm1 as varchar),4),2) +
':' +
right('0000' + cast(tm1 as varchar),2) as datetime)),7)
, right(convert(varchar,cast('01-Jan-1753 ' +
left(right('0000' + tm2,4),2) +
':' +
right('0000' + tm2,2) as datetime)),7)
from #tmp

and in anticipation of your next question...

select convert(varchar,cast(cast(dt1 as varchar) +
' ' +
left(right('0000' + cast(tm1 as varchar),4),2) +
':' +
right('0000' + cast(tm1 as varchar),2) as datetime), 100)
, convert(varchar,cast(substring(dt2,5,2) + '-' + right(dt2,2) + '-' + left(dt2,4) + ' ' +
left(right('0000' + cast(tm1 as varchar),4),2) +
':' +
right('0000' + cast(tm1 as varchar),2) as datetime), 100)
from #tmp|||Is there any easier way of doing this... this way seems very complicated !

Realtive query cost in the execution plan

Hello everyone!
I have a general question about measuring query cost while tuning
queries. If I put both queries (the old one and the modified one) into
the same batch, can I then use the query cost (relative to the batch)
percentage number as a reliable way of measuring whether the performance
has improved?
I would also appreciate any links to the good articles on this matter.
Thank you,
Igor
*** Sent via Developersdex http://www.examnotes.net ***that's a good way to start - if it really unbalanced (e.g., version 1 is
90% of the total cost), then i've typically satisfied as to which is best.
however, i've found the most accurate way to measure it is to get the
actual timings over at least 10 runs. if it's a really long query, this
may not be totally feasible
e.g.
declare @.x datetime
set @.x = getdate()
-- run version 1
select datediff(ms, @.x, getdate()) as Version1_timing
set @.x = getdate()
-- run version 2
select datediff(ms, @.x, getdate()) as Version2_timing
mEmENT0m0RI wrote:
> Hello everyone!
> I have a general question about measuring query cost while tuning
> queries. If I put both queries (the old one and the modified one) into
> the same batch, can I then use the query cost (relative to the batch)
> percentage number as a reliable way of measuring whether the performance
> has improved?
> I would also appreciate any links to the good articles on this matter.
> Thank you,
> Igor
>
> *** Sent via Developersdex http://www.examnotes.net ***|||Trey,
Thanks for your reply.
How about measuring the absolute query cost? Let's say I've added some
indexes and want to see the performance difference?
*** Sent via Developersdex http://www.examnotes.net ***|||by "absolute" do you mean what it actually uses rather than the estimate?
if so, run the queries again, but with the Show Execution Plan option
on. (CTRL-K is the shortcut).
this will show the actual query plan used - btw: you'll have to use this
option rather than the estimated query plan if temp tables are involved.
mEmENT0m0RI wrote:
> Trey,
> Thanks for your reply.
> How about measuring the absolute query cost? Let's say I've added some
> indexes and want to see the performance difference?
>
> *** Sent via Developersdex http://www.examnotes.net ***

Really need to use this query - but it has a sub-query in it...

Hi all,

I have the following query in my application and really need to run this as it will help my application in terms of performance and efficiency. I need to get the record that was last entered into the database, and the following seems to do it in SQL Server Desktop, but not in SQL Server Mobile (as it has a Sub-Query). At the moment I have a query that returns all rows (in order) that have a DateCompleted column entered and then just skimming the top row. So i'm infact bringing back hundreds of rows, just so that I can get the first record - completed waste resources if you ask me.

Does anyone know how I can tackle this issue?

SELECT *
FROM tbl_NSP_AnswerSet
WHERE (DateCompleted IS NOT NULL)
AND DateCompleted = (SELECT MAX(DateCompleted) FROM tbl_NSP_AnswerSet)
ORDER BY DateCompleted DES

Thanks

Tryst

Hi

Create a User Define function which will calculate for you the Max datecomleted for that table and then in the where clause compaire the datecompleted.

CREATE FUNCTION GetMaxDateCompleted()
RETURNS int
AS
BEGIN
(SELECT MAX(DateCompleted) FROM tbl_NSP_AnswerSet)
END

AND

SELECT *
FROM tbl_NSP_AnswerSet
WHERE (DateCompleted IS NOT NULL)
AND DateCompleted = dbo.GetMaxDateCompleted()
ORDER BY DateCompleted DES

|||But SQL Server mobile doesn't support Functions (UDF's), does it?

Tryst

Really need help with this tough query

Hello all. What I want to do has left me stumped. I will try to articulate everything, but please reply if I have omitted some details.

My goal is to effectively choose the next sequencial contract number as follows: there are two tables, each with a contract_number field, whose format is as follows:
ANNNA-NNNN, where A represents a single alphabetic character, and each N represents an integer.
What I need done is to chose the largest value occupying the last four digits of a contract number.

An example would clarify:

table1
C600D-1234
C500E-1231

table2
A400F-1235
G300I-1222

My query should return: A400F-1235 (or just 1235) since the last four digits are greatest in this instance.
I have a feeling that I will use the functions CAST, MAX, and SUBSTRING, but this is just too complicate for me. To make matters worse, there will be some obsolete ill-formatted numbers that should be ignored in case they do not conform to the format.

Even the smallest bit of advice could prove priceless.
Thanks!To make this problem a whole lot more manageable, forget the issue about there being two table; I can take care of that with my server-side code. So basically, I just need the maximum integer value retrieved from a column containing both numbers and letters, as described in the previous post. Again, however, I need a way to cope with (ignore) poorly-formatted contract numbers.

Thanks!

Originally posted by ujohnc00
Hello all. What I want to do has left me stumped. I will try to articulate everything, but please reply if I have omitted some details.

My goal is to effectively choose the next sequencial contract number as follows: there are two tables, each with a contract_number field, whose format is as follows:
ANNNA-NNNN, where A represents a single alphabetic character, and each N represents an integer.
What I need done is to chose the largest value occupying the last four digits of a contract number.

An example would clarify:

table1
C600D-1234
C500E-1231

table2
A400F-1235
G300I-1222

My query should return: A400F-1235 (or just 1235) since the last four digits are greatest in this instance.
I have a feeling that I will use the functions CAST, MAX, and SUBSTRING, but this is just too complicate for me. To make matters worse, there will be some obsolete ill-formatted numbers that should be ignored in case they do not conform to the format.

Even the smallest bit of advice could prove priceless.
Thanks!|||Try this

create table foo (test varchar(30))

insert into foo select 'abcd-1234' union all select 'dfrg-1235' union all select 'adgf-9999'

select test from foo where right(test,4) in (select max (right(test,4)) from foo)

HTH|||I think you would get faster performance using the = operator rather than the "in" operator:

where right(test,4) = (select max (right(test,4)) from foo)

...but unless the query is very large the difference is probably negligable.

Another minor point; you have the option of running your select clause from the union query directly, without using a temporary table if you want.

blindman|||Oops, absolutely right, you want to use the '=' not 'in'

Thanks|||Originally posted by rhigdon

insert into foo select 'abcd-1234' union all select 'dfrg-1235' union all select 'adgf-9999'

HTH

hi, i am new to all these db stuff
but wat does the above statement do

i don't get the
select 'abcd-1234' union all select 'dfrg-1235' union all select 'adgf-9999'
part

thanx|||Just a way to insert rows instead of saying

insert into table select 'blah'
insert into table select 'blah'
insert into table select 'blah'

Using the union all statement allows you to write one insert that accomodates multiple inserts.

HTH|||Originally posted by rhigdon
Just a way to insert rows instead of saying

insert into table select 'blah'
insert into table select 'blah'
insert into table select 'blah'

Using the union all statement allows you to write one insert that accomodates multiple inserts.

HTH

arhh... thanx,

is there any good resources that you gurus out would recommend me to read, either books or online resources

cheers
j|||BOL - http://microsoft.com/sql/techinfo/productdoc/2000/books.asp

Inside SQL Server - Kalen Delaney

Data and databases - Joe Celko

Guru's guide to transact SQL - Ken Henderson

HTH|||thank you all for your replies.

i think your suggestions will work for my situation, with one exception: some of the sequence numbers are poorly-formatted, and in that case, the "max (right(test,4))" may cause an error, or produce undesriable results. basically, if "right(test,4)" is not an integer, i don't want to deal with that record. any ideas of how to accomplish this?

also, blindman wrote:
"Another minor point; you have the option of running your select clause from the union query directly, without using a temporary table if you want."

Can you show the sql code for this (I can't picture what you mean)?

Thank you infinitely!|||You can use the isnumeric function.

Not tested but something like this:

if
isnumeric(right(test,4)) = 1
then|||This illustrates how to use UNION to combine both tables into a subquery, for both your outer select and for determining the largest current value. Whether you use this, a temporary table, or a table variable is a matter of choice and the details of your project.

This also shows how to use the ISNUMERIC function to screen out non-numeric bad data.

select test
from (select * from table1 Union select * from table2) foo
where right(test,4) =
(select max(right(test, 4))
from (select * from table1 Union select * from table2) foo2
where ISNUMERIC(right(test, 4))=1)

blindman|||blindman and rhigdon...

thanks infinitely for helping me out. i believe i now have exactly what i want thanks to you.

one curiosity: when we write SELECT MAX(RIGHT(...)), why doesn't some sort of cast to an integer need to take place (or does sql server do this implicitly)?

take care.|||You can use the Max and Min aggregate functions on non-numeric data, which is then evaluated according the sort-order of the character set. For numeric data stored as text, there is no difference in the result as long as all values have the same number of digits.
Of course, you can't use statistical functions like Avg with non-numeric data!

blindman|||I bet you thought this thread was dead... never!

anyways, thanks for the continual help.

as a slight modfication to my original question, imagine now that the formatting is in one of the following formats (where L's represent letters, and N's represent integers):

L600-NNN-LLLL
LL600-NNN-LLLL
LLL600-NNN-LLLL
LLLL600-NNN-LLLL

the question remains the same: how can I find the largest NNN in a column whose contents usually follow the above formatting? how can i ignore those numbers that don't conform to the above formatting? the reason that this formatting poses a new problem for me is that i can't use the RIGHT or LEFT functions, and therefore don't know how to parse out the NNN values.

thank you in advance for your time.
cheers|||Consider using the LIKE operator, along with wildcards, to filter out bad data. For example:
Where [DataString] like '%-[0-9][0-9][0-9]-[a-z][a-z][a-z][a-z]'
(This is untested, but you get the idea...)

You are going to need to familiarize yourself with the character functions available in TSQL: RIGHT, LEFT, TRIM, SUBSTRING, CHARINDEX, for starters.

To get the substring for your example, we'll nest two functions. The inner-most peels off the leftmost 8 characters, and the outer function grabs the left three from the result:

left(right([DataString], 8), 3)

blindman|||Is this a test?

We are really delving into the Sea of Volitility here...

If you ALWAYS have the hyphens, try this on for size:

SELECT MAX(LEFT(RIGHT(test,LEN(test)-CHARINDEX('-',test)),CHARINDEX(RIGHT(test,LEN(test)-CHARINDEX('-',test))-1)) FROM (SELECT test FROM table1) UNION ALL (SELECT test FROM table2)

I think a better solution would be to scold the person who created the contract numbering scheme and take it away from them.|||blindman and bpdWork,
thanks again for all of your guidance. you are the reason that the internet is such a vast resource.

i ended up following your suggestions, blindman, and things are working great. i will surely look into those functions you mentioned. bpdWork, I may have used your SQL but I couldn't parse it mentally, and that would be a bad thing if I needed to modify it later on.

cheers!|||bpdWork, rest assured that I have scolded those who create the contract numbering scheme, but unfortunately I don't have the power to take it away from them.

I do have yet another question for this thread (that has been so helpful thus far. The question is the same as the first one I posted on this thread, with the only difference being that I can't use the RIGHT function, because now the RIGHT four characters might not be the sequence number. This is because an options "-TO##" suffix may be appended to the contract number, where ## is a two digit integer between 00 and 99.

I expect to use CHARINDEX or maybe the CASE conditional, but I am not sure.

If I am bugging you with seemingly simple questions that aren't very different from eachother, it is only because I don't yet have full comprehension of the tools needed to write these types of queries (but you are helping a lot).

Thanks again!

Originally posted by bpdWork
Is this a test?

We are really delving into the Sea of Volitility here...

If you ALWAYS have the hyphens, try this on for size:

SELECT MAX(LEFT(RIGHT(test,LEN(test)-CHARINDEX('-',test)),CHARINDEX(RIGHT(test,LEN(test)-CHARINDEX('-',test))-1)) FROM (SELECT test FROM table1) UNION ALL (SELECT test FROM table2)

I think a better solution would be to scold the person who created the contract numbering scheme and take it away from them.|||I think you should use user-defined functions for this. I would write two: one to call that returns an integer, and one to parse the contract number into parts (it will be called from the first).

The parsing function is useful in many cases if written well. Here's one that'll accept a huge VARCHAR and a delimiter such as a hyphen (defaults to comma), and returns a numbered table of elements:

CREATE FUNCTION [fn_parse_string] (@.data_string VARCHAR(8000), @.delimiter VARCHAR(3)=',')
RETURNS @.element_table TABLE (element_id INT, element VARCHAR(512)) AS
BEGIN

DECLARE @.temp_table TABLE (element_id INT NOT NULL IDENTITY(1,1), element VARCHAR(512))
DECLARE @.iPos INT, @.element VARCHAR(512)

WHILE (LEN(@.data_string)>0) AND (@.iPos<>0)
BEGIN
SET @.iPos = CHARINDEX(@.delimiter, @.data_string)

IF @.iPos > 0
SET @.element = LEFT(@.data_string, @.iPos - 1)
ELSE
SET @.element = @.data_string

SET @.data_string = SUBSTRING(@.data_string, @.iPos + 1, LEN(@.data_string) - @.iPos)

INSERT @.temp_table (element)
VALUES (SELECT LTRIM(RTRIM(@.element)))

END

INSERT @.element_table
SELECT element_id, element
FROM @.temp_table

RETURN
END

Now you will want the function that will return the integer from a supplied contract code.

CREATE FUNCTION [fn_get_contract_number] (@.contract_code VARCHAR(50))
RETURNS INT AS
BEGIN
DECLARE @.contract_number AS INT
SELECT @.contract_number = CAST(element AS INT)
FROM dbo.fn_parse_string(@.contract_code)
WHERE element_id=2 -- if this is the part that holds the number
RETURN @.contract_number
END

Ok, it could use some error handling, as it will puke if element number two is not in integer, but you can play with that bit. Anyway, you could then use this function to do something like:

SELECT MAX(dbo.fn_get_contract_number(contracts.contract_ code)) FROM contracts

to get the Max number.

-b|||BTW: it will be slow, but you will have more managable code, and you will be able to exercise some more control without dizzying Select statements. Just get bigger processors!|||bpdWork, what a response!

I really appreciate your help. Your solution certainly seems flexible!

I am trying to figure out if this solution can handle these examples correctly:

V600P-1324 (will work)
V600P-1653-TO12 (will work)
V600P(C)-SAS-1653 (won't work as I need the 1653, not the SAS)
V600P(C)-SAS-1651-TO21 (won't work as I need the 1651)

I sincerely apologize for the continual complications on this question. The most simple way to put it is that if the RIGHT four characters or ints, then that is the number I want. If the RIGHT four character are TO##, then the four numbers that come before the TO## are what I want. I am sure that I could modify your code somehow to get this to work.

Thanks.|||Sure, simply modify the fn_get_contract_number to iterate through and return the first element that isnumeric().

If ISNUMERIC(element)...|||hmmm... not sure if I understand.

in all of the above contract number examples, I will need to extract the four digit number out of the whole string. wouldn't "If ISNUMBERIC" incorrectly skip some of the records?

thanks|||CREATE FUNCTION [fn_get_contract_number] (@.contract_code VARCHAR(50))
RETURNS INT AS
BEGIN
DECLARE @.contract_number AS INT
SELECT @.contract_number = CAST(element AS INT)
FROM dbo.fn_parse_string(@.contract_code)
WHERE ISNUMERIC(element)=1 -- only returns numeric elements
RETURN @.contract_number
END|||wow; i actually took 15 minutes to fully read through all of your code, and i fully understand what's going on. I have never used functions in SQL Server, but I can see how they can be useful. Any tips on when to use Sql Server functions?

i have really learned a lot from you and this thread.

thanks!