Showing posts with label rows. Show all posts
Showing posts with label rows. Show all posts

Thursday, March 29, 2012

Cursor update performance issue

I am using the following code which works fine except that when there are alot of rows being used (> 500) this performs really slow and I get a timeout error. Any ideas on how to make this faster since I need to update multiple rows, multiple times with multiple values?

<code>
DECLARE Item_Cursor CURSOR LOCAL FAST_FORWARD FOR
Select FileObjectId,CopyFileObjectId From FileObject
Where CopyFileObjectId IS NOT NULL
AND CreateTime = @.copyTime

set @.LastError = @.@.error
if(@.LastError <> 0) goto ERR_HANDLE

OPEN Item_Cursor
FETCH NEXT FROM Item_Cursor INTO @.NewId,@.OldId

WHILE @.@.FETCH_STATUS = 0
BEGIN
--update idhierarchies
Update FileObject Set IdHierarchy=Replace(IdHierarchy,'.'+Cast(@.OldId as varchar)+'.','.'+Cast(@.NewId as varchar)+'.')
Where CopyFileObjectId IS NOT NULL
AND CreateTime = @.copyTime

set @.LastError = @.@.error
if(@.LastError <> 0) goto ERR_HANDLE

--update parent ids
Update FileObject Set ParentId=@.NewId
Where ParentId=@.OldId
AND CopyFileObjectId IS NOT NULL
AND CreateTime = @.copyTime

set @.LastError = @.@.error
if(@.LastError <> 0) goto ERR_HANDLE

FETCH NEXT FROM Item_Cursor INTO @.NewId,@.OldId
END
CLOSE Item_Cursor
DEALLOCATE Item_Cursor

</code>I've read that when you declare a cursor the result of the select-statement actually gets written to the temp-database and that's whats causing you such delays. But from what I can read from your procedure here it should be possible to do those updates without the use of a cursor...?|||If you have any suggestions, let me know... I can't figure out how to do it in one or two update statements, that's definitely how I would prefer to do it and try to shoot for for every op. I just couldn't figur eout how to do that here.

Originally posted by Frettmaestro
I've read that when you declare a cursor the result of the select-statement actually gets written to the temp-database and that's whats causing you such delays. But from what I can read from your procedure here it should be possible to do those updates without the use of a cursor...?sql

Cursor Tuning Help!

This cursor is processing one row per second, all it is doing is appending a value to a column. There are 847,000 rows and I can't leave the query to run for 10 days! help!

CREATE procedure [dbo].sp_USR_TEMP_SESSIONAL_ATTENDANCE_APPEND
as

begin

DECLARE @.I_UPN varchar(13)
DECLARE @.I_SURNAME varchar(50)
DECLARE @.I_FORENAME varchar(50)
DECLARE @.I_DOB datetime
DECLARE @.I_GENDER varchar(1)
DECLARE @.I_LEA varchar(3)
DECLARE @.I_DFES varchar(4)
DECLARE @.I_ATTEND_YEAR varchar(4)
DECLARE @.I_WEEK_BEGINNING datetime
DECLARE @.I_ATTEND_CODES varchar(14)

declare @.cnt int

set nocount on

declare cur CURSOR

for select upn,
surname,
forename,
dob,
gender,
'353' as lea,
dfes,
attend_year,
week_beginning,
attend_codes
from tmpATTEND_IMPORT

for read only

open cur

fetch from cur into
@.I_UPN,
@.I_SURNAME,
@.I_FORENAME,
@.I_DOB,
@.I_GENDER,
@.I_LEA,
@.I_DFES,
@.I_ATTEND_YEAR,
@.I_WEEK_BEGINNING,
@.I_ATTEND_CODES

truncate table tmpATTEND_IMPORT_APPENDED

while @.@.fetch_status = 0
begin
set @.cnt = (select count(*) as cnt from tmpATTEND_IMPORT_APPENDED
where upn = @.I_UPN
and surname = @.I_SURNAME
and forename = @.I_FORENAME
and dob = @.I_DOB
and gender = @.I_GENDER
and lea = @.I_LEA
and dfes = @.I_DFES
and attend_year = @.I_ATTEND_YEAR)

if @.cnt = 0 insert tmpATTEND_IMPORT_APPENDED(
upn,
surname,
forename,
dob,
gender,
lea,
dfes,
attend_year,
week_beginning,
attend_codes)
values (
@.I_UPN,
@.I_SURNAME,
@.I_FORENAME,
@.I_DOB,
@.I_GENDER,
@.I_LEA,
@.I_DFES,
@.I_ATTEND_YEAR,
@.I_WEEK_BEGINNING,
@.I_ATTEND_CODES)

else update tmpATTEND_IMPORT_APPENDED
set attend_codes = attend_codes+@.I_ATTEND_CODES
where upn = @.I_UPN and
surname = @.I_SURNAME and
forename = @.I_FORENAME and
dob = @.I_DOB and
gender = @.I_GENDER and
lea = @.I_LEA and
dfes = @.I_DFES and
attend_year = @.I_ATTEND_YEAR

fetch next from cur into
@.I_UPN,
@.I_SURNAME,
@.I_FORENAME,
@.I_DOB,
@.I_GENDER,
@.I_LEA,
@.I_DFES,
@.I_ATTEND_YEAR,
@.I_WEEK_BEGINNING,
@.I_ATTEND_CODES

end

close cur

deallocate cur

end
GO

I tried adapting a post resolved on Romanian SQL Server User Group (www.sqlserver.ro) , to concatenate the columns

DECLARE @.MyTable TABLE (ID INT PRIMARY KEY, VAL VARCHAR(64))

INSERT INTO @.MyTable VALUES (1, 'Cristian')

INSERT INTO @.MyTable VALUES (2, 'Sorin')

INSERT INTO @.MyTable VALUES (3, 'Narcis')

DECLARE @.Lista VARCHAR(MAX)

SET @.Lista = '';

SELECT @.Lista=@.Lista + VAL + ' ' FROM @.MyTable

SELECT RTRIM(@.Lista) AS Lista

GO

-- Output

Lista

Cristian Sorin Narcis

so try the following procedure :

CREATE procedure [dbo].sp_USR_TEMP_SESSIONAL_ATTENDANCE_APPEND
as

begin

DECLARE @.I_UPN varchar(13)
DECLARE @.I_SURNAME varchar(50)
DECLARE @.I_FORENAME varchar(50)
DECLARE @.I_DOB datetime
DECLARE @.I_GENDER varchar(1)
DECLARE @.I_LEA varchar(3)
DECLARE @.I_DFES varchar(4)
DECLARE @.I_ATTEND_YEAR varchar(4)
DECLARE @.I_WEEK_BEGINNING datetime
DECLARE @.I_ATTEND_CODES varchar(14)


set nocount on

declare cur CURSOR

//select only distinct rows

for select upn,
surname,
forename,
dob,
gender,
'353' as lea,
dfes,
attend_year,
week_beginning,
from tmpATTEND_IMPORT
group by upn,surname,forename,dob,gender,lea,dfes,attend_year

for read only

open cur

fetch from cur into
@.I_UPN,
@.I_SURNAME,
@.I_FORENAME,
@.I_DOB,
@.I_GENDER,
@.I_LEA,
@.I_DFES,
@.I_ATTEND_YEAR,
@.I_WEEK_BEGINNING,


truncate table tmpATTEND_IMPORT_APPENDED

while @.@.fetch_status = 0
begin
//adding to @.I_ATTEND_CODES only rows that respect where clause
select @.I_ATTEND_CODES=attend_codes+@.I_ATTEND_CODES
from tmpATTEND_IMPORT
where upn = @.I_UPN and
surname = @.I_SURNAME and
forename = @.I_FORENAME and
dob = @.I_DOB and
gender = @.I_GENDER and
lea = @.I_LEA and
dfes = @.I_DFES and
attend_year = @.I_ATTEND_YEAR

// and insert what it find and add

insert tmpATTEND_IMPORT_APPENDED(
upn,
surname,
forename,
dob,
gender,
lea,
dfes,
attend_year,
week_beginning,
attend_codes)
values (
@.I_UPN,
@.I_SURNAME,
@.I_FORENAME,
@.I_DOB,
@.I_GENDER,
@.I_LEA,
@.I_DFES,
@.I_ATTEND_YEAR,
@.I_WEEK_BEGINNING,
@.I_ATTEND_CODES)

fetch next from cur into
@.I_UPN,
@.I_SURNAME,
@.I_FORENAME,
@.I_DOB,
@.I_GENDER,
@.I_LEA,
@.I_DFES,
@.I_ATTEND_YEAR,
@.I_WEEK_BEGINNING,


end

close cur

cursor to insert into a table variable

I would appreciate some help in writing a cursor to populate a table variable based on a result set
All the cursor does is find all rows with same area ID and areaName and insert into the table variable the one with the latest modified date, if all modified dates are equal then insert the one with the latest created date.
This is the result set(id int, areaID int, areaName varchar(30), created datetime, modifieddatetime):
3 10 MA 2005-03-04 2005-03-04
6 10 MA 2005-05-04 2005-03-04
9 10 MA 2005-03-04 2005-03-04
1 52 PUERTO RICO 2005-03-04 2005-03-04
2 52 PUERTO RICO 2005-03-04 2005-03-04
4 52 PUERTO RICO 2005-03-04 2005-05-04
8 52 PUERTO RICO 2005-03-04 2005-03-04
5 5 NY 2005-04-04 2005-03-04
7 5 NY 2005-03-04 2005-03-04
table variable @.tempTable(id int, areaID int, areaName varchar(30), created datetime, modified datetime)
After running this cursor my temp table would contain:
6 10 MA 2005-05-04 2005-03-04
4 52 PUERTO RICO 2005-03-04 2005-05-04
7 5 NY 2005-03-04 2005-03-04
Any help would be appreciated

you don't need a cursor for this at all
this should do it, change YourTable to your table name

insert into @.tempTable(id ,areaID ,areaName , created , modified )
select t1.* from
YourTable t1 join(
select areaID ,areaName , max(created)as MaxCreated,max( modified) as MaxModified
from YourTable
group by areaID ,areaName) t2 on
t1.areaID = t2.areaID
and t1.areaName = t2.areaName
and t1.created = t2.MaxCreated
and t1.modified = t2.MaxModified

if it is possible that you can have created and modified that are the same for an area then you have to group on t1 and grab the min or max id


Denis the SQL Menace
http://sqlservercode.blogspot.com/

|||

The following query will do..

Select TwoCol.* From
TwoCol
Join
(
Select
IdWise.ColOne,
IdMaxModdate,
IdMaxCrdDate
From


(Select ColOne,ModifieddateTime,Count(*) ModDateCount from TwoCol
group By ColOne,ModifieddateTime) as ModDateWise

Join (Select ColOne,Count(*) IdCount,max(ModifieddateTime) IdMaxModdate, max(createddatetime) IdMaxCrdDate
From TwoCol Group By ColOne) as IdWise On ModDateWise.ColOne = IdWise.ColOne And IdMaxModdate = ModifieddateTime
) as T On T.ColOne = TwoCol.ColOne And ModifieddateTime = IdMaxModdate And createddatetime = IdMaxCrdDate

Cursor Query?

Does anyone have any links on how to iterate over a set of rows in Sql 2005, I have never did this before...If someone would be so kind to write a basic cursor query just so i can atleast see how its implemented? Any links on how to create one?

In general, I try to avoid cursors; as a generalization I would suggest that set-based operations are usually faster than record-based operations. If you need to use a cursor, look up cursors in books online. What specifically are you trying to do?

You might want to give a look to this article about a TABLE OF NUMBERS; at times, this is useful:

http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-numbers-table.html

Another thing is that many things can be "iterated through" just simply by joining one table to another.

|||If you are really going to use a cursor, look at books online under "FETCH". It has a full set of examples.|||Well i have a column called sort_order and the sort_order should be 12345678910 etc.... but the sort order got jacked up not its like 112233445668899 so I wanted to write a cursor to go through every row and where there is an duplicate replace it with the appropiate count.....Do you have a better way to do this?|||My knee-jerk reaction to this partial description is that a cursor is not best. How are you planning on computing the new sort_id if you have a duplicate sort id?|||

Well what happen is that my sort_order column got thrown off due to some other issues....the computing of the sorting is correct when new items that need to sorted are created....its just we have some exsisting items that sort_order is thrown off due to other issues...Thats why i wanted to create a cursor to get all the exsisting sort orders and correct them by executing a sql script that use a cursor to iterate over all the ones who sort order is screwed to and correct them....does that make more sense? what would you suggest....I have an update query right now that will reset a particular set of items sort orders back to normal and everything is great but i think that take too long to do for all the sets of items that i have that need to be sorted.....what do think?

Now when i run this against a set of items that sort is off this will set the sort back to 12345678910111213 etc..... but i have alot of items to do this with, and that was the reason for the idea of creating the cursor.....would i be able to do this another way?

Declare @.intcounter int

set @.intcounter = 0

UPDATE tableName

SET @.intcounter = sort_order = @.intcounter + 1

FROM TableName AS st LEFT OUTER JOIN

TableName1 ON st.id = TableName.ID

WHERE (st.is_active = 1) AND (TableName1.ID = 13132)

|||

I am puzzling over the LEFT OUTER JOIN with the WHERE ... AND (TableName1.ID = 13132). Doesn't the AND condition force this into basically being an INNER JOIN?

Also, since the only contribution from TableName1 is to filter based on ID, do you even need to join to this table? Can't you just filter based on ST.ID = 13132?

If you don't have to join to the OUTER (or maybe INNER) table, this should be a rather efficient update -- provided you have an index on the ID column.

Please check me on this.

|||This was part of a bigger Sql statement that i had executed...As you can see I am not a sql GURU...lol but It works for what i need it to do...I just need it to work for every indiviudal ID...I dont want to have to enter in all the different ID's to fix the sort order...thats why i thought to use the cursor to fix this issue.....cause from what i see i can get it to iterate over all the rows and update the sort order for the ID in a table....|||

Consider doing it like this:

Declare @.intcounter int
set @.intcounter = 0

UPDATE tableName
SET @.intcounter = @.intcounter + 1,
sort_order = @.intcounter
WHERE is_active = 1
AND ID = 13132

This should iterate through the records you want fine and provide a sequence of number 1-N for N records that meet the WHERE / AND criteria. It will be efficient PROVIDED that you have an index on the ID column.

|||

I guess I sort-of need to make a confession here, too: I tend to loathe cursors -- I guess it is kind of a personal prejudice, but in this case I definitely have a bias and it is probably better if you are aware of that bias. I'd rather not go into that bias, but it is better for you if you know that this particular bias is present.


Kent

|||

I have one more question........I have a table with two columns..... the id column is not a primary key

column1ID column2_Numbers

1 2

1 2

1 3

1 4

1 5

1 6

1 7

In this table you see id 1 has some numbers that are duplicated......I want to know if this table was filled with a set of ids that have these numbers associated with them how would i go about counting all the IDS that had duplicate numbers associated with them? like since ID 1 has dups then my count should increase to 1 and so on?

|||

One way to count the number of column1IDs that have duplicates is something like:

select count(*)
from ( select column1ID,
count(*) as idCount
from yourTable
group by columnID
having count(*) > 1
) x

|||

what does the "x" do and why would i use the Having clause instead of doing it like

Select Count(id) From tableName

Where number_column = number_column?

|||

The "x" is arbitrary; you must give an arbitrary name to a derived table (the expression in the parends); I just happened to choose "x"; it could be any name. I am considering the "number_column" issue; that looks like something i do not yet understand.

Yes, that is definitely a misunderstanding by me. My last SELECT code is wrong; sorry, I will see about fixing that.

|||

I think it needs to look more like:

select count(*)
from ( select column1ID,
column2_numbers,
count(*) as idCount
from yourTable
group by columnID, column2_numbers
having count(*) > 1
) Y

The code:

Select Count(id) From tableName
Where number_column = number_column?

will not work as you wish; this code will select all rows of the table where NUMBER_COLUMN is not null

I suppose if you are wanting to find the NUMBER_COLUMN for a given ID you can do:

select column2_numbers,
count(*) as idCount
from yourTable
where column1ID = 1
group by column2_numbers
having count(*) > 1

Tuesday, March 27, 2012

cursor or while loop?

hello,
i have 160,000 rows in a table. need to send out an email to somebody
for each row.
currently using a cursor to loop through the rows and calling COM email
component to send the emails
ran low on resources after about 100,000 records
would it be better to do something like this?
DECLARE @.Rowcount int
SELECT @.RowCount=COUNT(*) from table
WHILE @.RowCount > 0
BEGIN
SELECT TOP 1 @.somevar=somevar FROM table
SET @.RowCount = @.@.ROWCOUNT
IF @.Rowcount > 0
BEGIN
send the email
delete the record
END
END
this would only grab one record at a time. any recommendations?
TIA
NeilPass the result set to the front end and do not try to do this in the
database.|||Check out http://www.sqldev.net there is a xp_smtp_sendmail which you can
use to send your mail.
You will need to use a cursor still because there isn't anything that takes
a set.
I'd create a temporary table or table variable of the rows you want to email
first, then send the emails and join back removing the rows; you might want
recoverability in which case use a permenent table or something similar -
you don't want to lose the position if the server goes down.
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials
<neilmcguigan@.gmail.com> wrote in message
news:1131321608.259481.84490@.g49g2000cwa.googlegroups.com...
> hello,
> i have 160,000 rows in a table. need to send out an email to somebody
> for each row.
> currently using a cursor to loop through the rows and calling COM email
> component to send the emails
> ran low on resources after about 100,000 records
> would it be better to do something like this?
> DECLARE @.Rowcount int
> SELECT @.RowCount=COUNT(*) from table
> WHILE @.RowCount > 0
> BEGIN
> SELECT TOP 1 @.somevar=somevar FROM table
> SET @.RowCount = @.@.ROWCOUNT
> IF @.Rowcount > 0
> BEGIN
> send the email
> delete the record
> END
> END
> this would only grab one record at a time. any recommendations?
> TIA
> Neil
>|||That just won't scale.
The number of round trips 160,000 would be even slower.
This is one of many examples where keeping it inside the SQL Server engine
scales better and is more maintable (you don't have to code using two
different langauges and worry about a front end app).
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials
"--CELKO--" <jcelko212@.earthlink.net> wrote in message
news:1131322861.083235.253060@.g43g2000cwa.googlegroups.com...
> Pass the result set to the front end and do not try to do this in the
> database.
>|||I really had a lot of reliability problems with sql server mail.
I just don't trust mapi based mail anymore.
xp_smtp_sendmail is dead reliable. I pump huge amounts through it everyday.
What are we going to do with Yukon?
Tony, do you know if xp's written for 2000 will work with 2005 or is the
native mail OK in 2005?
Paul
"Tony Rogerson" <tonyrogerson@.sqlserverfaq.com> wrote in message
news:ekRlvE34FHA.2548@.TK2MSFTNGP10.phx.gbl...
> Check out http://www.sqldev.net there is a xp_smtp_sendmail which you can
> use to send your mail.
> You will need to use a cursor still because there isn't anything that
> takes a set.
> I'd create a temporary table or table variable of the rows you want to
> email first, then send the emails and join back removing the rows; you
> might want recoverability in which case use a permenent table or something
> similar - you don't want to lose the position if the server goes down.
> --
> Tony Rogerson
> SQL Server MVP
> http://sqlserverfaq.com - free video tutorials
>
> <neilmcguigan@.gmail.com> wrote in message
> news:1131321608.259481.84490@.g49g2000cwa.googlegroups.com...
>|||Hi Paul,
Believe me - they got the message with MAPI mail!
Its been rewritten (again), but this time properly - MAPI is only there for
backward compatibility, there is a new SMTP mail built into SQL Server 2005
and its highly scalable too (at last!).
I will be glad to see the back of Outlook 2000 and SQL Server.
Tony.
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials
"Paul Cahill" <xyzpaul.xyzcahill@.dsl.pipex.com> wrote in message
news:OEEASy34FHA.1140@.tk2msftngp13.phx.gbl...
>I really had a lot of reliability problems with sql server mail.
> I just don't trust mapi based mail anymore.
> xp_smtp_sendmail is dead reliable. I pump huge amounts through it
> everyday.
> What are we going to do with Yukon?
> Tony, do you know if xp's written for 2000 will work with 2005 or is the
> native mail OK in 2005?
> Paul
>
> "Tony Rogerson" <tonyrogerson@.sqlserverfaq.com> wrote in message
> news:ekRlvE34FHA.2548@.TK2MSFTNGP10.phx.gbl...
>|||Additional...
You've really got to get your head round what the SQL Server product is now,
its not just a RDBMS its a whole host of things that we can take advantage
of when building applications, be they simple or complex.
You don't have to go off to DB2 etc... in order to build a dimensional
model, you can use Analysis Services which is part of the SQL Server product
set and has been since 2000.
You don't have to go off and buy another text search package, you can use
Full-Text indexing which is built into the product as well.
Please (oh please), bring your knowledge up-to-date; you are so far behind
its embarrasing.
Tony Rogerson
SQL Server MVP
http://sqlserverfaq.com - free video tutorials
"--CELKO--" <jcelko212@.earthlink.net> wrote in message
news:1131322861.083235.253060@.g43g2000cwa.googlegroups.com...
> Pass the result set to the front end and do not try to do this in the
> database.
>|||I'm going to have to kindof disagree with you on this one. Making a
round-trip on each result would be a horrendous solution; however,
returing a full batch 160,000 records to the client wouldn't be bad at
all (even if the client were the SQL Server machine.). I consider my
databases to be dynamic entities whose purpose is efficient storage,
retrieval and manipulation of data. I wouldn't dream of calling a COM
component from SQL Server; even the idea of embedding .NET assemblies
in 2005 scares me a bit.
The two solutions I would consider using would be embedded an ActiveX
script in a SQL Server Agent job step. I prefer Python, however,
VBScript and JScript are readily available for use. Or you can easily
throw together a quick .NET console app.
EXAMPLE (Compile using: csc /t:exe SendEMails.cs):
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mail;
class SendEmails
{
static void Main(string[] args)
{
SqlDataAdapter da = new SqlDataAdapter(@."
SELECT EmailAddress
FROM TableName
", "server=.;database=MyDatabase;integrated security=sspi;");
DataTable dt = new DataTable();
da.Fill(dt);
SmtpMail.SmtpServer =
"smtp.domain.com";
foreach (DataRow dr in dt.Rows)
SmtpMail.Send("from@.domain.com", dr["EmailAddress"].ToString(),
"Subject", "MessageText");
}
}
-Alan|||Thanks Tony that's good news.
Now I've only got to remember how those DTS tasks I wrote last year work :)
Paul
"Tony Rogerson" <tonyrogerson@.sqlserverfaq.com> wrote in message
news:O8r7xG44FHA.2888@.tk2msftngp13.phx.gbl...
> Hi Paul,
> Believe me - they got the message with MAPI mail!
> Its been rewritten (again), but this time properly - MAPI is only there
> for backward compatibility, there is a new SMTP mail built into SQL Server
> 2005 and its highly scalable too (at last!).
> I will be glad to see the back of Outlook 2000 and SQL Server.
> Tony.
> --
> Tony Rogerson
> SQL Server MVP
> http://sqlserverfaq.com - free video tutorials
>
> "Paul Cahill" <xyzpaul.xyzcahill@.dsl.pipex.com> wrote in message
> news:OEEASy34FHA.1140@.tk2msftngp13.phx.gbl...
>|||thanks for all the info guys.
i need something that can run continuously, reliably, minimal resouce
use
thinking i might go to an external program, write a .net service
perhaps.
maybe grab a couple hundred records at a time, process them, update.
i liked the all sql model as it didn't seem to use too many resources.
but i like the error handing in .net better.

Sunday, March 25, 2012

Cursor help please

Hello. I'm am trying to write a cursor in SQL Server 2000 that looks in a table with a list of tablenames (UpdateTables), looks up the number of rows in that table and then enters the number of rows in the second column of the lookup table against the correct tablename. I have written the following but am getting an error message saying that I need to declare the variable @.tblName

GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[sp_UpdateTablesUpdate]
as
DECLARECrExtractUpdateCount CURSOR FOR SELECT Tablename FROM dbo.UpdateTables
DECLARE @.tblName char(50)

OPEN CrExtractUpdateCount
FETCH NEXT FROM CrExtractUpdateCount
INTO @.tblName

WHILE @.@.FETCH_STATUS = 0
BEGIN
UPDATE UpdateTables
SET Number_Rows = (select count (*) from @.tblName)
WHERE dbo.[UpdateTables].[Tablename] = @.tblName
FETCH NEXT FROM CrExtractUpdateCount
INTO @.tblName
END
CLOSE CrExtractUpdateCount
DEALLOCATE CrExtractUpdateCount

GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

any help much appreciated.Hi,

Variable should be handled as follows.
go throu. the following lines for example..

DECLARE @.tblName char(50)
set @.tblName='Employee' --Table name
DECLARE @.count char(500)
set @.count='(select count (*) from ' + rtrim(isnull(@.tblName,0)) + ')'
PRINT @.count

Then Use the variable, @.count to update the second column.
Hope U understand.

Regards,
Sharmila|||Thanks for help but am now getting bad syntax errors around
set @.count='(select count (*) from ' + rtrim(isnull(@.tblName,0)) + ')'|||The two SQL statements I have tried now are:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_UpdateTablesUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_UpdateTablesUpdate]
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[sp_UpdateTablesUpdate]
as
DECLARECrExtractUpdateCount CURSOR FOR SELECT Tablename FROM dbo.UpdateTables
DECLARE @.tblName char(50)
DECLARE @.NoRows char(50)

OPEN CrExtractUpdateCount
FETCH NEXT FROM CrExtractUpdateCount
INTO @.tblName

WHILE @.@.FETCH_STATUS = 0
BEGIN
UPDATE UpdateTables
SET Number_Rows = 'select count (*) from @.tblName'
WHERE dbo.[UpdateTables].[Tablename] = @.tblName
FETCH NEXT FROM CrExtractUpdateCount
INTO @.tblName
END
CLOSE CrExtractUpdateCount
DEALLOCATE CrExtractUpdateCount

GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

The above piece of code simply puts the text select count (*) from @.tblName in each row in destination table.
There fore I tried:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_UpdateTablesUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_UpdateTablesUpdate]
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[sp_UpdateTablesUpdate]
as
DECLARECrExtractUpdateCount CURSOR FOR SELECT Tablename FROM dbo.UpdateTables
DECLARE @.tblName char(50)
DECLARE @.NoRows char(50)

OPEN CrExtractUpdateCount
FETCH NEXT FROM CrExtractUpdateCount
INTO @.tblName

WHILE @.@.FETCH_STATUS = 0
BEGIN
UPDATE UpdateTables
SET Number_Rows = (select count (*) from @.tblName)
WHERE dbo.[UpdateTables].[Tablename] = @.tblName
FETCH NEXT FROM CrExtractUpdateCount
INTO @.tblName
END
CLOSE CrExtractUpdateCount
DEALLOCATE CrExtractUpdateCount

GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

Which is giving error
Server: Msg 137, Level 15, State 2, Procedure sp_UpdateTablesUpdate, Line 14
Must declare the variable '@.tblName'.

I don't understand this as the variable is working perfectly in the next line.

I also tried putting the select statement into a variable called @.NoRows which didn't work either (same two results as above)

Anymore help much appreciated.|||Thanks for the help.
Managed to crack it by putting @.NoRows into a temp table temprows then updating UpdateTables from this. As follows

if exists (select * from dbo.sysobjects where id =

object_id(N'[dbo].[cp_UpdateTablesUpdate]') and OBJECTPROPERTY(id,

N'IsProcedure') = 1)
drop procedure [dbo].[cp_UpdateTablesUpdate]
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[cp_UpdateTablesUpdate]
as
DECLARECrExtractUpdateCount CURSOR FOR SELECT Tablename FROM

dbo.UpdateTables
DECLARE @.tblName char(50)
DECLARE @.NoRows char(50)
DECLARE @.NoRowsC char(50)

OPEN CrExtractUpdateCount
FETCH NEXT FROM CrExtractUpdateCount
INTO @.tblName

WHILE @.@.FETCH_STATUS = 0
BEGIN
set @.NoRows = 'select count (*) from ' + @.tblName
insert into temprows exec (@.NoRows)
UPDATE UpdateTables
SET Number_Rows = temprows.execreturn
from temprows
WHERE dbo.[UpdateTables].[Tablename] = @.tblName
FETCH NEXT FROM CrExtractUpdateCount
INTO @.tblName
END
CLOSE CrExtractUpdateCount
DEALLOCATE CrExtractUpdateCount

GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

exec cp_UpdateTablesUpdate

Cursor for a result set returned by a stored proc

Hi all,

i got a question. I have a stored procedure sp1 calling another stored procedure sp2 using Exec sp2 cmd.

sp2 returns 2 rows in the result set.
Now I want to loop thru those 2 rows and get access to its column values
from sp1. How can this be acheived?

thanks for your help

omair-I came across this after reading your post:

One legitimate use for temp tables is to use them to pass recordsets from a nested stored procedure to a calling stored procedure. This is the only way to pass recordsets from one stored procedure to another. When you do this, follow the other tips on this Web site to maximize temp table performance. [6.5, 7.0, 2000] Updated 3-6-2006

Hope it helps.

Hannah

Cursor count

Hi!

How can I get the size of a cursor variable (the number of rows in it)?

Thank you!

Hi,

use @.@.CURSOR_ROWS

HTH, jens Suessmeyer.

http://www.sqlserver2005.de

Wednesday, March 21, 2012

current row no?

i know there is a function to count number of rows in a group, but is there anyway of numbering each row?

So that I can use =Last(ReportIems("norow") to find the last row on the page?

Thanks

Try putting this into your detail row (where GroupName is the name of your group):

=RowNumber("GroupName")

Hope this helps.

Jarret

|||Thank you SO much.... I used this to a Page no for each group ;)

current row no?

i know there is a function to count number of rows in a group, but is there anyway of numbering each row?

So that I can use =Last(ReportIems("norow") to find the last row on the page?

Thanks

Try putting this into your detail row (where GroupName is the name of your group):

=RowNumber("GroupName")

Hope this helps.

Jarret

|||Thank you SO much.... I used this to a Page no for each group ;)

Sunday, March 11, 2012

Curious performance experiment

I found an SP scanning a table of 100k rows which lacked the proper
index, doing a join to several other tables. Statistics IO reported
it took 26000 logical reads (to do it twice, btw).
I then added the proper index. Reads dropped to 1500. Good, huh? I
should have declared victory and gone home.
But no, not me. I then dropped the index and reran. It went up
again, of course, but only to 6700 reads.
Um, ... anyone ever see anything like this? I'm at something of a
loss. I thought I'd run it several times in each mode, and in fact
was running it in a loop of 100 from query analyzer to get a decent
clock-time idea of execution (but got the statistics IO from single
executions).
Josh
"JXStern" <JXSternChangeX2R@.gte.net> wrote in message
news:kduho09q2pojpejabtn73p13d8m3lq2jnu@.4ax.com...
>I found an SP scanning a table of 100k rows which lacked the proper
> index, doing a join to several other tables. Statistics IO reported
> it took 26000 logical reads (to do it twice, btw).
> I then added the proper index. Reads dropped to 1500. Good, huh? I
> should have declared victory and gone home.
> But no, not me. I then dropped the index and reran. It went up
> again, of course, but only to 6700 reads.
> Um, ... anyone ever see anything like this? I'm at something of a
> loss. I thought I'd run it several times in each mode, and in fact
> was running it in a loop of 100 from query analyzer to get a decent
> clock-time idea of execution (but got the statistics IO from single
> executions).
>
Was the index perhaps clustered? If so the data would have have been
reorganized by clustering and unclustering it, perhaps making the joins more
efficient.
Also you may have ended up with better statistics after adding and dropping
the index, which enabled a better query plan.
David
|||On Wed, 3 Nov 2004 10:17:07 -0600, "David Browne" <davidbaxterbrowne
no potted meat@.hotmail.com> wrote:
>Was the index perhaps clustered? If so the data would have have been
>reorganized by clustering and unclustering it, perhaps making the joins more
>efficient.
Not clustered.

>Also you may have ended up with better statistics after adding and dropping
>the index, which enabled a better query plan.
But without the index, it has to scan.
It *appears* that the "logical reads" are not all that logical, and
are affected by the data cached. I've seen this phenomenon again
today. It is upsetting. Unless there is some larger principle at
work here I have not yet understood?
Josh
|||JXStern wrote:
> I found an SP scanning a table of 100k rows which lacked the proper
> index, doing a join to several other tables. Statistics IO reported
> it took 26000 logical reads (to do it twice, btw).
> I then added the proper index. Reads dropped to 1500. Good, huh? I
> should have declared victory and gone home.
> But no, not me. I then dropped the index and reran. It went up
> again, of course, but only to 6700 reads.
> Um, ... anyone ever see anything like this? I'm at something of a
> loss. I thought I'd run it several times in each mode, and in fact
> was running it in a loop of 100 from query analyzer to get a decent
> clock-time idea of execution (but got the statistics IO from single
> executions).
> Josh
What are these"reads"? Are you using STATISTS IO for the information? If
so, how many logical vs physical? You might consider using Profiler to
see the reads, CPU, and duration. Don't be alarmed that the reads in
Profiler do not match that of STATISTICS IO. They are reading different
structures and the SQL Trace API will normally report reads that are not
tracked by STATISTICS IO.
David Gugick
Imceda Software
www.imceda.com
|||JxStern
Before starting to tune the query after re-creating an index run the
following commands
DBCC FREEPROCCACHE clears the procedure cache and causes ad hoc queries to
be recompiled
if you want a stored procedure to be compiled you will need to use the WITH
RECOMPILE option
if you want to clear the data cache you will need to use DBCC
DROPCLEANBUFFERS
DBCC FLUSHPROCINDB: Used to clear out the stored procedure cache for a
specific database on a SQL Server, not the entire SQL Server. The database
ID number to be affected must be entered as part of the command.
"JXStern" <JXSternChangeX2R@.gte.net> wrote in message
news:kduho09q2pojpejabtn73p13d8m3lq2jnu@.4ax.com...
> I found an SP scanning a table of 100k rows which lacked the proper
> index, doing a join to several other tables. Statistics IO reported
> it took 26000 logical reads (to do it twice, btw).
> I then added the proper index. Reads dropped to 1500. Good, huh? I
> should have declared victory and gone home.
> But no, not me. I then dropped the index and reran. It went up
> again, of course, but only to 6700 reads.
> Um, ... anyone ever see anything like this? I'm at something of a
> loss. I thought I'd run it several times in each mode, and in fact
> was running it in a loop of 100 from query analyzer to get a decent
> clock-time idea of execution (but got the statistics IO from single
> executions).
> Josh
>
|||On Thu, 4 Nov 2004 09:26:32 +0200, "Uri Dimant" <urid@.iscar.co.il>
wrote:
>JxStern
>Before starting to tune the query after re-creating an index run the
>following commands
>DBCC FREEPROCCACHE clears the procedure cache and causes ad hoc queries to
>be recompiled
>if you want a stored procedure to be compiled you will need to use the WITH
>RECOMPILE option
>if you want to clear the data cache you will need to use DBCC
>DROPCLEANBUFFERS
>DBCC FLUSHPROCINDB: Used to clear out the stored procedure cache for a
>specific database on a SQL Server, not the entire SQL Server. The database
>ID number to be affected must be entered as part of the command.
All good suggestions.
I did use sp_recompile, but it didn't seem to make any difference.
Which is additionally odd, isn't it? Could it have cached the plan
and the old index? But it didn't return the old (with index) value,
but an intermediate one.
J.
|||On Wed, 3 Nov 2004 19:47:13 -0500, "David Gugick"
<davidg-nospam@.imceda.com> wrote:
>What are these"reads"? Are you using STATISTS IO for the information?
Yes, statistics io, and I'm looking at just the logical reads.

> If
>so, how many logical vs physical?
Lots of logical, very few physical.

>You might consider using Profiler to
>see the reads, CPU, and duration.
I've been running the profiler also, but didn't use it on these cases.

> Don't be alarmed that the reads in
>Profiler do not match that of STATISTICS IO. They are reading different
>structures and the SQL Trace API will normally report reads that are not
>tracked by STATISTICS IO.
I'm just comparing runs via statistics io at this point.
Interesting if profiler shows a different pattern, I'll try to post if
I get the chance to try it.
So, nobody is quite going with my theory that the logical reads aren't
purely logical?
J.

Curious performance experiment

I found an SP scanning a table of 100k rows which lacked the proper
index, doing a join to several other tables. Statistics IO reported
it took 26000 logical reads (to do it twice, btw).
I then added the proper index. Reads dropped to 1500. Good, huh? I
should have declared victory and gone home.
But no, not me. I then dropped the index and reran. It went up
again, of course, but only to 6700 reads.
Um, ... anyone ever see anything like this? I'm at something of a
loss. I thought I'd run it several times in each mode, and in fact
was running it in a loop of 100 from query analyzer to get a decent
clock-time idea of execution (but got the statistics IO from single
executions).
Josh"JXStern" <JXSternChangeX2R@.gte.net> wrote in message
news:kduho09q2pojpejabtn73p13d8m3lq2jnu@.4ax.com...
>I found an SP scanning a table of 100k rows which lacked the proper
> index, doing a join to several other tables. Statistics IO reported
> it took 26000 logical reads (to do it twice, btw).
> I then added the proper index. Reads dropped to 1500. Good, huh? I
> should have declared victory and gone home.
> But no, not me. I then dropped the index and reran. It went up
> again, of course, but only to 6700 reads.
> Um, ... anyone ever see anything like this? I'm at something of a
> loss. I thought I'd run it several times in each mode, and in fact
> was running it in a loop of 100 from query analyzer to get a decent
> clock-time idea of execution (but got the statistics IO from single
> executions).
>
Was the index perhaps clustered? If so the data would have have been
reorganized by clustering and unclustering it, perhaps making the joins more
efficient.
Also you may have ended up with better statistics after adding and dropping
the index, which enabled a better query plan.
David|||On Wed, 3 Nov 2004 10:17:07 -0600, "David Browne" <davidbaxterbrowne
no potted meat@.hotmail.com> wrote:
>Was the index perhaps clustered? If so the data would have have been
>reorganized by clustering and unclustering it, perhaps making the joins more
>efficient.
Not clustered.
>Also you may have ended up with better statistics after adding and dropping
>the index, which enabled a better query plan.
But without the index, it has to scan.
It *appears* that the "logical reads" are not all that logical, and
are affected by the data cached. I've seen this phenomenon again
today. It is upsetting. Unless there is some larger principle at
work here I have not yet understood?
Josh|||JXStern wrote:
> I found an SP scanning a table of 100k rows which lacked the proper
> index, doing a join to several other tables. Statistics IO reported
> it took 26000 logical reads (to do it twice, btw).
> I then added the proper index. Reads dropped to 1500. Good, huh? I
> should have declared victory and gone home.
> But no, not me. I then dropped the index and reran. It went up
> again, of course, but only to 6700 reads.
> Um, ... anyone ever see anything like this? I'm at something of a
> loss. I thought I'd run it several times in each mode, and in fact
> was running it in a loop of 100 from query analyzer to get a decent
> clock-time idea of execution (but got the statistics IO from single
> executions).
> Josh
What are these"reads"? Are you using STATISTS IO for the information? If
so, how many logical vs physical? You might consider using Profiler to
see the reads, CPU, and duration. Don't be alarmed that the reads in
Profiler do not match that of STATISTICS IO. They are reading different
structures and the SQL Trace API will normally report reads that are not
tracked by STATISTICS IO.
--
David Gugick
Imceda Software
www.imceda.com|||JxStern
Before starting to tune the query after re-creating an index run the
following commands
DBCC FREEPROCCACHE clears the procedure cache and causes ad hoc queries to
be recompiled
if you want a stored procedure to be compiled you will need to use the WITH
RECOMPILE option
if you want to clear the data cache you will need to use DBCC
DROPCLEANBUFFERS
DBCC FLUSHPROCINDB: Used to clear out the stored procedure cache for a
specific database on a SQL Server, not the entire SQL Server. The database
ID number to be affected must be entered as part of the command.
"JXStern" <JXSternChangeX2R@.gte.net> wrote in message
news:kduho09q2pojpejabtn73p13d8m3lq2jnu@.4ax.com...
> I found an SP scanning a table of 100k rows which lacked the proper
> index, doing a join to several other tables. Statistics IO reported
> it took 26000 logical reads (to do it twice, btw).
> I then added the proper index. Reads dropped to 1500. Good, huh? I
> should have declared victory and gone home.
> But no, not me. I then dropped the index and reran. It went up
> again, of course, but only to 6700 reads.
> Um, ... anyone ever see anything like this? I'm at something of a
> loss. I thought I'd run it several times in each mode, and in fact
> was running it in a loop of 100 from query analyzer to get a decent
> clock-time idea of execution (but got the statistics IO from single
> executions).
> Josh
>|||On Thu, 4 Nov 2004 09:26:32 +0200, "Uri Dimant" <urid@.iscar.co.il>
wrote:
>JxStern
>Before starting to tune the query after re-creating an index run the
>following commands
>DBCC FREEPROCCACHE clears the procedure cache and causes ad hoc queries to
>be recompiled
>if you want a stored procedure to be compiled you will need to use the WITH
>RECOMPILE option
>if you want to clear the data cache you will need to use DBCC
>DROPCLEANBUFFERS
>DBCC FLUSHPROCINDB: Used to clear out the stored procedure cache for a
>specific database on a SQL Server, not the entire SQL Server. The database
>ID number to be affected must be entered as part of the command.
All good suggestions.
I did use sp_recompile, but it didn't seem to make any difference.
Which is additionally odd, isn't it? Could it have cached the plan
and the old index? But it didn't return the old (with index) value,
but an intermediate one.
J.|||On Wed, 3 Nov 2004 19:47:13 -0500, "David Gugick"
<davidg-nospam@.imceda.com> wrote:
>What are these"reads"? Are you using STATISTS IO for the information?
Yes, statistics io, and I'm looking at just the logical reads.
> If
>so, how many logical vs physical?
Lots of logical, very few physical.
>You might consider using Profiler to
>see the reads, CPU, and duration.
I've been running the profiler also, but didn't use it on these cases.
> Don't be alarmed that the reads in
>Profiler do not match that of STATISTICS IO. They are reading different
>structures and the SQL Trace API will normally report reads that are not
>tracked by STATISTICS IO.
I'm just comparing runs via statistics io at this point.
Interesting if profiler shows a different pattern, I'll try to post if
I get the chance to try it.
So, nobody is quite going with my theory that the logical reads aren't
purely logical'
J.

Curious performance experiment

I found an SP scanning a table of 100k rows which lacked the proper
index, doing a join to several other tables. Statistics IO reported
it took 26000 logical reads (to do it twice, btw).
I then added the proper index. Reads dropped to 1500. Good, huh? I
should have declared victory and gone home.
But no, not me. I then dropped the index and reran. It went up
again, of course, but only to 6700 reads.
Um, ... anyone ever see anything like this? I'm at something of a
loss. I thought I'd run it several times in each mode, and in fact
was running it in a loop of 100 from query analyzer to get a decent
clock-time idea of execution (but got the statistics IO from single
executions).
Josh"JXStern" <JXSternChangeX2R@.gte.net> wrote in message
news:kduho09q2pojpejabtn73p13d8m3lq2jnu@.
4ax.com...
>I found an SP scanning a table of 100k rows which lacked the proper
> index, doing a join to several other tables. Statistics IO reported
> it took 26000 logical reads (to do it twice, btw).
> I then added the proper index. Reads dropped to 1500. Good, huh? I
> should have declared victory and gone home.
> But no, not me. I then dropped the index and reran. It went up
> again, of course, but only to 6700 reads.
> Um, ... anyone ever see anything like this? I'm at something of a
> loss. I thought I'd run it several times in each mode, and in fact
> was running it in a loop of 100 from query analyzer to get a decent
> clock-time idea of execution (but got the statistics IO from single
> executions).
>
Was the index perhaps clustered? If so the data would have have been
reorganized by clustering and unclustering it, perhaps making the joins more
efficient.
Also you may have ended up with better statistics after adding and dropping
the index, which enabled a better query plan.
David|||On Wed, 3 Nov 2004 10:17:07 -0600, "David Browne" <davidbaxterbrowne
no potted meat@.hotmail.com> wrote:
>Was the index perhaps clustered? If so the data would have have been
>reorganized by clustering and unclustering it, perhaps making the joins mor
e
>efficient.
Not clustered.

>Also you may have ended up with better statistics after adding and dropping
>the index, which enabled a better query plan.
But without the index, it has to scan.
It *appears* that the "logical reads" are not all that logical, and
are affected by the data cached. I've seen this phenomenon again
today. It is upsetting. Unless there is some larger principle at
work here I have not yet understood?
Josh|||JXStern wrote:
> I found an SP scanning a table of 100k rows which lacked the proper
> index, doing a join to several other tables. Statistics IO reported
> it took 26000 logical reads (to do it twice, btw).
> I then added the proper index. Reads dropped to 1500. Good, huh? I
> should have declared victory and gone home.
> But no, not me. I then dropped the index and reran. It went up
> again, of course, but only to 6700 reads.
> Um, ... anyone ever see anything like this? I'm at something of a
> loss. I thought I'd run it several times in each mode, and in fact
> was running it in a loop of 100 from query analyzer to get a decent
> clock-time idea of execution (but got the statistics IO from single
> executions).
> Josh
What are these"reads"? Are you using STATISTS IO for the information? If
so, how many logical vs physical? You might consider using Profiler to
see the reads, CPU, and duration. Don't be alarmed that the reads in
Profiler do not match that of STATISTICS IO. They are reading different
structures and the SQL Trace API will normally report reads that are not
tracked by STATISTICS IO.
David Gugick
Imceda Software
www.imceda.com|||JxStern
Before starting to tune the query after re-creating an index run the
following commands
DBCC FREEPROCCACHE clears the procedure cache and causes ad hoc queries to
be recompiled
if you want a stored procedure to be compiled you will need to use the WITH
RECOMPILE option
if you want to clear the data cache you will need to use DBCC
DROPCLEANBUFFERS
DBCC FLUSHPROCINDB: Used to clear out the stored procedure cache for a
specific database on a SQL Server, not the entire SQL Server. The database
ID number to be affected must be entered as part of the command.
"JXStern" <JXSternChangeX2R@.gte.net> wrote in message
news:kduho09q2pojpejabtn73p13d8m3lq2jnu@.
4ax.com...
> I found an SP scanning a table of 100k rows which lacked the proper
> index, doing a join to several other tables. Statistics IO reported
> it took 26000 logical reads (to do it twice, btw).
> I then added the proper index. Reads dropped to 1500. Good, huh? I
> should have declared victory and gone home.
> But no, not me. I then dropped the index and reran. It went up
> again, of course, but only to 6700 reads.
> Um, ... anyone ever see anything like this? I'm at something of a
> loss. I thought I'd run it several times in each mode, and in fact
> was running it in a loop of 100 from query analyzer to get a decent
> clock-time idea of execution (but got the statistics IO from single
> executions).
> Josh
>|||On Thu, 4 Nov 2004 09:26:32 +0200, "Uri Dimant" <urid@.iscar.co.il>
wrote:
>JxStern
>Before starting to tune the query after re-creating an index run the
>following commands
>DBCC FREEPROCCACHE clears the procedure cache and causes ad hoc queries to
>be recompiled
>if you want a stored procedure to be compiled you will need to use the WITH
>RECOMPILE option
>if you want to clear the data cache you will need to use DBCC
>DROPCLEANBUFFERS
>DBCC FLUSHPROCINDB: Used to clear out the stored procedure cache for a
>specific database on a SQL Server, not the entire SQL Server. The database
>ID number to be affected must be entered as part of the command.
All good suggestions.
I did use sp_recompile, but it didn't seem to make any difference.
Which is additionally odd, isn't it? Could it have cached the plan
and the old index? But it didn't return the old (with index) value,
but an intermediate one.
J.|||On Wed, 3 Nov 2004 19:47:13 -0500, "David Gugick"
<davidg-nospam@.imceda.com> wrote:
>What are these"reads"? Are you using STATISTS IO for the information?
Yes, statistics io, and I'm looking at just the logical reads.

> If
>so, how many logical vs physical?
Lots of logical, very few physical.

>You might consider using Profiler to
>see the reads, CPU, and duration.
I've been running the profiler also, but didn't use it on these cases.

> Don't be alarmed that the reads in
>Profiler do not match that of STATISTICS IO. They are reading different
>structures and the SQL Trace API will normally report reads that are not
>tracked by STATISTICS IO.
I'm just comparing runs via statistics io at this point.
Interesting if profiler shows a different pattern, I'll try to post if
I get the chance to try it.
So, nobody is quite going with my theory that the logical reads aren't
purely logical'
J.

Saturday, February 25, 2012

cube measures on rows in reporting services....

Is there anyway for reporting services to display measures on rows instead
of columns as in Excel?
I'm trying to format my data in rows but this doesn't seems possible in
reporting services... does anyone has any examples of how this can be done?Create a shared datasource to Foodmart 200 and try this RDL. HEre is a sample
report that i created with measures on rows.
<?xml version="1.0" encoding="utf-8"?>
<Report
xmlns="http://schemas.microsoft.com/sqlserver/reporting/2003/10/reportdefinition"
xmlns:rd="">http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
<RightMargin>1in</RightMargin>
<Body>
<ReportItems>
<Textbox Name="textbox1">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<FontFamily>Times New Roman</FontFamily>
<BackgroundColor>Brown</BackgroundColor>
<BorderWidth>
<Bottom>3pt</Bottom>
</BorderWidth>
<BorderColor>
<Bottom>Black</Bottom>
</BorderColor>
<BorderStyle>
<Bottom>Solid</Bottom>
</BorderStyle>
<FontSize>18pt</FontSize>
<TextAlign>Center</TextAlign>
<Color>White</Color>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
<FontWeight>700</FontWeight>
</Style>
<ZIndex>1</ZIndex>
<rd:DefaultName>textbox1</rd:DefaultName>
<Height>0.33in</Height>
<CanGrow>true</CanGrow>
<Value>Report20</Value>
</Textbox>
<Matrix Name="matrix1">
<Corner>
<ReportItems>
<Textbox Name="textbox4">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
</Style>
<ZIndex>5</ZIndex>
<rd:DefaultName>textbox4</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value />
</Textbox>
</ReportItems>
</Corner>
<Height>0.72in</Height>
<Style />
<MatrixRows>
<MatrixRow>
<MatrixCells>
<MatrixCell>
<ReportItems>
<Textbox Name="textbox2">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
</Style>
<ZIndex>2</ZIndex>
<rd:DefaultName>textbox2</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value>=First(Fields!Measures_Unit_Sales.Value)</Value>
</Textbox>
</ReportItems>
</MatrixCell>
</MatrixCells>
<Height>0.24in</Height>
</MatrixRow>
<MatrixRow>
<MatrixCells>
<MatrixCell>
<ReportItems>
<Textbox Name="textbox3">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
</Style>
<rd:DefaultName>textbox3</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value>=First(Fields!Measures_Profit.Value)</Value>
</Textbox>
</ReportItems>
</MatrixCell>
</MatrixCells>
<Height>0.24in</Height>
</MatrixRow>
</MatrixRows>
<MatrixColumns>
<MatrixColumn>
<Width>1in</Width>
</MatrixColumn>
</MatrixColumns>
<DataSetName>FoodMart_2000</DataSetName>
<ColumnGroupings>
<ColumnGrouping>
<DynamicColumns>
<Grouping Name="matrix1_Customers_Country">
<GroupExpressions>
<GroupExpression>=Fields!Customers_Country.Value</GroupExpression>
</GroupExpressions>
</Grouping>
<Sorting>
<SortBy>
<SortExpression>=Fields!Customers_Country.Value</SortExpression>
<Direction>Ascending</Direction>
</SortBy>
</Sorting>
<ReportItems>
<Textbox Name="Customers_Country">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<FontFamily>Times New Roman</FontFamily>
<FontSize>12pt</FontSize>
<Color>DarkRed</Color>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
<FontWeight>700</FontWeight>
</Style>
<ZIndex>4</ZIndex>
<rd:DefaultName>Customers_Country</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value>=Fields!Customers_Country.Value</Value>
</Textbox>
</ReportItems>
</DynamicColumns>
<Height>0.24in</Height>
</ColumnGrouping>
</ColumnGroupings>
<Width>2in</Width>
<Top>0.33in</Top>
<RowGroupings>
<RowGrouping>
<Width>1in</Width>
<StaticRows>
<StaticRow>
<ReportItems>
<Textbox Name="textbox7">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<FontFamily>Times New Roman</FontFamily>
<FontSize>12pt</FontSize>
<Color>DarkRed</Color>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
<FontWeight>700</FontWeight>
</Style>
<ZIndex>3</ZIndex>
<rd:DefaultName>textbox7</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value>Units</Value>
</Textbox>
</ReportItems>
</StaticRow>
<StaticRow>
<ReportItems>
<Textbox Name="textbox8">
<Style>
<PaddingLeft>2pt</PaddingLeft>
<FontFamily>Times New Roman</FontFamily>
<FontSize>12pt</FontSize>
<Color>DarkRed</Color>
<PaddingBottom>2pt</PaddingBottom>
<PaddingTop>2pt</PaddingTop>
<PaddingRight>2pt</PaddingRight>
<FontWeight>700</FontWeight>
</Style>
<ZIndex>1</ZIndex>
<rd:DefaultName>textbox8</rd:DefaultName>
<CanGrow>true</CanGrow>
<Value>Profit</Value>
</Textbox>
</ReportItems>
</StaticRow>
</StaticRows>
</RowGrouping>
</RowGroupings>
</Matrix>
</ReportItems>
<Style />
<Height>2.25in</Height>
</Body>
<TopMargin>1in</TopMargin>
<DataSources>
<DataSource Name="FoodMart 2000">
<DataSourceReference>FoodMart 2000</DataSourceReference>
</DataSource>
</DataSources>
<Width>5in</Width>
<DataSets>
<DataSet Name="FoodMart_2000">
<Fields>
<Field Name="Customers_Country">
<DataField>[Customers].[Country].[MEMBER_CAPTION]</DataField>
<rd:TypeName>System.String</rd:TypeName>
</Field>
<Field Name="Measures_Unit_Sales">
<DataField>[Measures].[Unit Sales]</DataField>
<rd:TypeName>System.Object</rd:TypeName>
</Field>
<Field Name="Measures_Profit">
<DataField>[Measures].[Profit]</DataField>
<rd:TypeName>System.Object</rd:TypeName>
</Field>
</Fields>
<Query>
<DataSourceName>FoodMart 2000</DataSourceName>
<CommandText>select
{{[Customers].[Country].Members}}on rows,
{ [Measures].[Unit Sales],[Measures].[Profit]} on columns
from Sales</CommandText>
</Query>
</DataSet>
</DataSets>
<LeftMargin>1in</LeftMargin>
<rd:SnapToGrid>true</rd:SnapToGrid>
<rd:DrawGrid>true</rd:DrawGrid>
<BottomMargin>1in</BottomMargin>
<Language>en-US</Language>
</Report>
"Nestor" wrote:
> Is there anyway for reporting services to display measures on rows instead
> of columns as in Excel?
> I'm trying to format my data in rows but this doesn't seems possible in
> reporting services... does anyone has any examples of how this can be done?
>
>

Friday, February 24, 2012

Cube browse in SSMS slow to start

I have a cube that has a couple hundred million rows and when I begin a cube browse, it takes a very long time for it to start. I profiled it and looked at the query it was running. It was querying for (what looked like) the totals for all measures at the All level for every dimension.

First of all, I would expect this to not take too long on my cube (no aggs) but I stopped the query after 45 minutes. (I know that adding some aggs to the cube would help and I will do that eventually.)

Second, how come does this query need to be run at the beginning of a browse? When the browse starts, there isn't anything in the data section of the pivot table.

How come is this necessary?

Mark

http://mgarner.wordpress.com

Try to see if this situation has anything to do with OWC.

Try to use MDX Editor in SQL Managemet studio. See what is taking most of the time.

Is it loading of metadata?

Try to send simple MDX query to bring only totals from your cube. Something like:

Select measures.members on 0 from [MyCube]

Run Profiler and see where the time is spent. 45 minutes looks bit excessive.

Edward.
--
This posting is provided "AS IS" with no warranties, and confers no rights.

|||

Edward,

Thanks so much for your response.

I agree that 45 mins is a bit excessive. It could also be something that is wrong with my cube design, but I wanted to get some info about this start-up query anyways.

select filter([Measures].members, [Measures].currentmember.member_caption = [Measures].currentmember.member_caption) on columns from [myCube]

This is the query that is running when I start a browse session in SSMS. I noticed that the query looks the same and the same operations are executed when I run the query in an MDX query window as well. It takes the same amount of time.

Do you happen to know why this query is run each time a browse window is open?

I also noticed (in Profiler) that each time this query is run that it doesn't look like it is ever retrieved from cache. Is there some reason why this query can't be put into cahce? If not, can you think of a reason why my cube design might keep it from being placed in cache?

Also, when I take out the vast majority of my partitions and only leave 4 (which would be amost 10 million fact row lines) with 15% aggregations, the query takes about 1 minute. This also could be due to my server and/or SAN configuration, but I would still expect that a query that just returns the totals for all the measures at the [All] level for every dimension wouldn't take too long.

Thanks in advance,

Mark

http://mgarner.wordpress.com