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

Tuesday, March 27, 2012

Can I force SQL Server to use the CONTAINS operator first?

If I do the query below, SQL Server does a table scan (thousands of rows) for fn_TestCol(), then evaluates the CONTAINS clause:

SELECT col1, col2
FROM myTable
WHERE CONTAINS((col1, col2), 'foo and bar')
AND fn_TestCol(col1) = 0

How can I force it to evaluate CONTAINS clause, which returns only a few rows, first? The best I've come up with is this:

SELECT sub.col1, sub.col2
FROM (
SELECT col1, col2
FROM myTable
WHERE CONTAINS((col1, col2), 'foo and bar')
) sub
WHERE fn_TestCol(sub.col1) = 0

It's much faster, but still not as fast as if I could just use the first query, but force SQL Server to evaluate CONTAINS first.

Actually CONTAINS is a predicate that can be used with the WHERE clause there are two versions of it defined by ANSI SQL and implemented by Microsoft CONTAINS and CONTAINSTABLE, the other two FULLTEXT predicates are FREETEXT and FREETEXTTBALE, try the link below for details. Hope this helps.

http://technet.microsoft.com/en-us/library/ms187787.aspx

|||

Hi Caddre,

Thanks for the reply -- but that didn't seem to have anything to do with my question!

|||

(It's much faster, but still not as fast as if I could just use the first query, but force SQL Server to evaluate CONTAINS first.)

It does because since CONTAINS is a predicate and not a clause what you can force it to do is determined by the restrictions and limitations listed in the link I provided.

|||no, you will have no control over the freetext functionality as in SQL Server 2005 the query in FT searches still is not connected to the query engine and therefore cannot be optimized and tweaked in your requested way.

Jens K. Suessmeyer.

http://www.sqlserver2005.de
|||

OK, I found a way to do it:

DECLARE @.ftQuery nvarchar(32)

SELECT col1, col2
FROM myTable
WHERE CONTAINS((col1, col2), @.ftQuery)
AND fn_TestCol(col1) = 0

OPTION (OPTIMIZE FOR(@.ftQuery = 'foo'))


This structure allows the optimizer to make assumptions about what might be in @.ftQuery -- so it correctly assumes that the CONTAINS predicate will be a faster starting point than the udf, and runs with the right plan.

|||

_jesse,
Interesting.. this as long been a "by design" problem with SQL FTS since SQL Server 7.0. You never said which version of SQL Server you are using, but I'm assuming it is SQL 2000 and not SQL Server 2005. Correct? Can you provide more details on your UDF fn_TestCol? How many rows do you have in your real table or your myTable example?

Thanks,
John

John T. Kane
Search Evangelist, Intellisearch
email: john.kane@.intellisearch.no

|||

Hi John,

It is SQL 2005.

The problem, as I understand it, is this: when you pass a parameter to CONTAINS, as in

CONTAINS(col1, @.searchText)

the optimizer isn't able to recognize that the CONTAINS operator is a good place to start. The execution plan changes if you replace @.searchText with a literal string, as in:

CONTAINS(col1, 'foo')

In my case, the table being scanned was relatively small (20,000 rows), but the UDF is slow -- it's doing a bunch of string manipulation & comparison. Since the CONTAINS clause is likely to be very selective, the query is very fast if you start with CONTAINS, then run the UDF on the few rows matching the fts, but very slow if you do it the other way around.

Declaring the @.searchText parameter as nvarchar, then adding the OPTIMIZE FOR hint gets it to consistently run in the right order.

Sunday, March 25, 2012

can I export view result to excel ?

Good morning all,

I created a view that selects the rows of the records from a table that matches where condition.
I need to export the records to excel so I could send it out...
what is the best way to do?
Please tell me detail, I don't know much about SQL 2000, still learning.

Thank you,
YanorooUse the DTS wizard (import and export data in start menu->Microsoft SQL server).

The wizard will ask you the information needed in order to complete the operation. Select the MS-Excel driver as a destination (as well as the output filename), choose the first options (export tables an views) and select your view in particular.

Shouldn't be too hard.|||yes, I was able to do that!
Thank you!

Thursday, March 22, 2012

Can I create new rows by using spreadsheet with less columns?

I wondering if I can create new rows in a table that has 48 columns by impor
ting data in a spreadsheet that has only 4 columns?yes, assuming the 44 columns that won't have data allow nulls or have defaul
t values assigned to them.
joel wrote:

> I wondering if I can create new rows in a table that has 48 columns by importing d
ata in a spreadsheet that has only 4 columns?|||I did it using the DTS designer - worked great.
Thanks
Joel

Tuesday, March 20, 2012

can i calculate a database backup size?

Hi,
I've a database with multiple millions of rows in a small set of tables.
Most of this data is historical, so I want to split the data in multiple
databases, make backups of about 600 MB each and burn this to cd roms. I
want to split the data based on dates stored inside the tables. Is there a
way to calculate the amount of bytes inside a database backup file that will
be produced ba a diven select statement when it uses all rows of a table?
SvenBackups aren't caused by select statements. They backup all of the physical
data in the database unless you do file or filegroup backups. So if you
want to get an idea of the size the backup will be you only have to look at
how much actual data (not including free space) there is in the db and it
will be approximately that. You can compress backups quite easily with
WINZIP or similar utilities and fit much more on a cd.
--
Andrew J. Kelly
SQL Server MVP
"Sven Erik Matzen" <sven.matzen@.dontspamme.com> wrote in message
news:eJ3BY7XZDHA.1832@.TK2MSFTNGP09.phx.gbl...
> Hi,
> I've a database with multiple millions of rows in a small set of tables.
> Most of this data is historical, so I want to split the data in multiple
> databases, make backups of about 600 MB each and burn this to cd roms. I
> want to split the data based on dates stored inside the tables. Is there a
> way to calculate the amount of bytes inside a database backup file that
will
> be produced ba a diven select statement when it uses all rows of a table?
> Sven
>|||Good point Dan. Be sure to run UPDATE USAGE or use the Updateusage option
when running it.
--
Andrew J. Kelly
SQL Server MVP
"Dan Guzman" <danguzman@.nospam-earthlink.net> wrote in message
news:%23Cs0mbZZDHA.2572@.TK2MSFTNGP12.phx.gbl...
> To add to Andrew's response, the reserved space reported by sp_spaceused
> can provide a rough estimate of database backup space requirements.
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> --
> SQL FAQ links (courtesy Neil Pike):
> http://www.ntfaq.com/Articles/Index.cfm?DepartmentID=800
> http://www.sqlserverfaq.com
> http://www.mssqlserver.com/faq
> --
> "Sven Erik Matzen" <sven.matzen@.dontspamme.com> wrote in message
> news:eJ3BY7XZDHA.1832@.TK2MSFTNGP09.phx.gbl...
> > Hi,
> >
> > I've a database with multiple millions of rows in a small set of
> tables.
> > Most of this data is historical, so I want to split the data in
> multiple
> > databases, make backups of about 600 MB each and burn this to cd roms.
> I
> > want to split the data based on dates stored inside the tables. Is
> there a
> > way to calculate the amount of bytes inside a database backup file
> that will
> > be produced ba a diven select statement when it uses all rows of a
> table?
> >
> > Sven
> >
> >
>|||Sven,
That's a really awkward way of going about it. Why not just BCP out the
data to flat files (I suggest native mode) and load them onto the CD(s).
You can use normal select statements to break them up into what ever you
want. It will be much cleaner and easier than attempting to do this with
backups. As for the size, it would be difficult to tell exactly unless all
your columns are of the fixed size (no varchars, text etc.). I think with a
few test selects you should be able to get a pretty good idea though.
--
Andrew J. Kelly
SQL Server MVP
"Sven Erik Matzen" <sven.matzen@.dontspamme.com> wrote in message
news:u9WCdCkZDHA.2520@.TK2MSFTNGP09.phx.gbl...
> I plan to split up database. 99% of the size of my database is caused by
> "Table1". What I need to know is: How many rows do I have to delete to be
> able to produce a backup os ~600MByte. I need functional database backups
to
> be able to restore data from a specific date.
> The idea was to have a second database with all the data but Table1, then
> - do a move of x rows from the original database to the new database
> - then make a backup
> - truncate Table1 of the backed up database
> - repeat these steps until all data is backed up.
> Now I need to know what type of relationship is there between space used
in
> a database and space used in a backup file. I don't want to split one huge
> database backup file to burn it on cdroms, because then I need to restore
> the complete database from 100 CDs just to lookup one row - that's not
what
> I had in mind ;).
>
> "Andrew J. Kelly" <sqlmvpnooospam@.shadhawk.com> wrote in message
> news:eQAY7jYZDHA.3444@.tk2msftngp13.phx.gbl...
> > Backups aren't caused by select statements. They backup all of the
> physical
> > data in the database unless you do file or filegroup backups. So if you
> > want to get an idea of the size the backup will be you only have to look
> at
> > how much actual data (not including free space) there is in the db and
it
> > will be approximately that. You can compress backups quite easily with
> > WINZIP or similar utilities and fit much more on a cd.
> >
> > --
> >
> > Andrew J. Kelly
> > SQL Server MVP
> >
> >
> > "Sven Erik Matzen" <sven.matzen@.dontspamme.com> wrote in message
> > news:eJ3BY7XZDHA.1832@.TK2MSFTNGP09.phx.gbl...
> > > Hi,
> > >
> > > I've a database with multiple millions of rows in a small set of
tables.
> > > Most of this data is historical, so I want to split the data in
multiple
> > > databases, make backups of about 600 MB each and burn this to cd roms.
I
> > > want to split the data based on dates stored inside the tables. Is
there
> a
> > > way to calculate the amount of bytes inside a database backup file
that
> > will
> > > be produced ba a diven select statement when it uses all rows of a
> table?
> > >
> > > Sven
> > >
> > >
> >
> >
>

Sunday, March 11, 2012

Can fields span TableGroups independantly of columns

Hi,

I'm trying to add a heading to my report using Table and TableGroups. But I don't want my group headings to line up with the detail rows. This seems like a common requirement in my mind but I can't figure out how to do it.

See made up example of a report below:



Person Name Address <-Report header

Area: A12 3BC, Newtown, Someplace <-TableGroup header


Mr Some Body 1 The Street <-Detail rows

Mrs Anne Other 2 The Street


Area: B23 4CD, Oldtown, Someplace <-TableGroup header


Mr Ran Dom 3 The Avenue <-Detail rows


So the TableGroup displays Area which is grouped by Post code, city etc.

The detail rows displays individual people and their street address.

NOTE: The Area fields do not line up with the Person fields in fact they overlap how can I do this in SSRS?

Thanks in advance for your help,

Mike G

I've found out how to do this now...

You need to have a single column in the table that spans the whole width of the report, (as opposed to having one column in the table control for each field)

Then add rectangle controls to each of the cells (Report header, table group headers and details etc.)

Then you can add textbox controls onto the rectangle controls and put them where you like within the cell.

It's a shame you have to do it this way because you loose the nice feature that lines up the details columns with the labels you've added in the report heading.

Hope that all makes sense, it's difficult to explain graphical layout issues in text!

-MIke G

|||

have you tried the list control?

Can fields span TableGroups independantly of columns

Hi,

I'm trying to add a heading to my report using Table and TableGroups. But I don't want my group headings to line up with the detail rows. This seems like a common requirement in my mind but I can't figure out how to do it.

See made up example of a report below:


Person Name Address <-Report header


Area: A12 3BC, Newtown, Someplace <-TableGroup header


Mr Some Body 1 The Street <-Detail rows

Mrs Anne Other 2 The Street


Area: B23 4CD, Oldtown, Someplace <-TableGroup header


Mr Ran Dom 3 The Avenue <-Detail rows


So the TableGroup displays Area which is grouped by Post code, city etc.

The detail rows displays individual people and their street address.

NOTE: The Area fields do not line up with the Person fields in fact they overlap how can I do this in SSRS?

Thanks in advance for your help,

Mike G

I've found out how to do this now...

You need to have a single column in the table that spans the whole width of the report, (as opposed to having one column in the table control for each field)

Then add rectangle controls to each of the cells (Report header, table group headers and details etc.)

Then you can add textbox controls onto the rectangle controls and put them where you like within the cell.

It's a shame you have to do it this way because you loose the nice feature that lines up the details columns with the labels you've added in the report heading.

Hope that all makes sense, it's difficult to explain graphical layout issues in text!

-MIke G

|||

have you tried the list control?

Can fields span TableGroups independantly of columns

Hi,

I'm trying to add a heading to my report using Table and TableGroups. But I don't want my group headings to line up with the detail rows. This seems like a common requirement in my mind but I can't figure out how to do it.

See made up example of a report below:



Person Name Address <-Report header

Area: A12 3BC, Newtown, Someplace <-TableGroup header


Mr Some Body 1 The Street <-Detail rows

Mrs Anne Other 2 The Street


Area: B23 4CD, Oldtown, Someplace <-TableGroup header


Mr Ran Dom 3 The Avenue <-Detail rows


So the TableGroup displays Area which is grouped by Post code, city etc.

The detail rows displays individual people and their street address.

NOTE: The Area fields do not line up with the Person fields in fact they overlap how can I do this in SSRS?

Thanks in advance for your help,

Mike G

I've found out how to do this now...

You need to have a single column in the table that spans the whole width of the report, (as opposed to having one column in the table control for each field)

Then add rectangle controls to each of the cells (Report header, table group headers and details etc.)

Then you can add textbox controls onto the rectangle controls and put them where you like within the cell.

It's a shame you have to do it this way because you loose the nice feature that lines up the details columns with the labels you've added in the report heading.

Hope that all makes sense, it's difficult to explain graphical layout issues in text!

-MIke G

|||have you tried the list control?

Thursday, March 8, 2012

can cursor be nested?

Hi, I'm a newbie on using cursor. My question is if I create an update
trigger that loop through the Inserted rows by a cursor, and inside the
cursor loop, a stored procedure is executed, which contains a cursor loop
too, any problem about it?YEs of course, you can also nest cursors inline.
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"nonno" <nonno@.discussions.microsoft.com> schrieb im Newsbeitrag
news:8EFFAC69-6544-4923-8A41-F228688A2F93@.microsoft.com...
> Hi, I'm a newbie on using cursor. My question is if I create an update
> trigger that loop through the Inserted rows by a cursor, and inside the
> cursor loop, a stored procedure is executed, which contains a cursor loop
> too, any problem about it?|||Thx for ur reply :) But if the outer cursor loop and the inner cursor loop
both access the same table, will deadlock occur?
"Jens Sü?meyer" wrote:

> YEs of course, you can also nest cursors inline.
> HTH, Jens Suessmeyer.
> --
> http://www.sqlserver2005.de
> --
> "nonno" <nonno@.discussions.microsoft.com> schrieb im Newsbeitrag
> news:8EFFAC69-6544-4923-8A41-F228688A2F93@.microsoft.com...
>
>|||Have a look at the Cursor option in BOL, you can handle the outside cusors
to behave as readonly if you need to. Rember that the default locking
beahviour is row locking, so even you will lock the data with anyother
option it depends on the the option wheter you lock one or multiple
datarows.
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"nonno" <nonno@.discussions.microsoft.com> schrieb im Newsbeitrag
news:9D8BD362-E3F9-4E1A-80BF-819CD58B2D85@.microsoft.com...
> Thx for ur reply :) But if the outer cursor loop and the inner cursor loop
> both access the same table, will deadlock occur?
> "Jens Smeyer" wrote:
>|||another question:
if I open a cursor in a transaction and the transaction rollback, will the
cursor be automatically closed and deallocated?
"Jens Sü?meyer" wrote:

> Have a look at the Cursor option in BOL, you can handle the outside cusors
> to behave as readonly if you need to. Rember that the default locking
> beahviour is row locking, so even you will lock the data with anyother
> option it depends on the the option wheter you lock one or multiple
> datarows.
> HTH, Jens Suessmeyer.
> --
> http://www.sqlserver2005.de
> --
> "nonno" <nonno@.discussions.microsoft.com> schrieb im Newsbeitrag
> news:9D8BD362-E3F9-4E1A-80BF-819CD58B2D85@.microsoft.com...
>
>|||Because of the termination of the session inthat case, that would be the
effect.
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"nonno" <nonno@.discussions.microsoft.com> schrieb im Newsbeitrag
news:C8DF1B00-5A10-413D-83A8-2986CBE6B4A2@.microsoft.com...
> another question:
> if I open a cursor in a transaction and the transaction rollback, will the
> cursor be automatically closed and deallocated?
> "Jens Smeyer" wrote:
>|||So to keep up your questions, it depends...
The inner Cursor will be closed when the session ends, the session ends when
the whole logic block is executed or an serverity error occured that kept
SQl Server from continuing the Cursor and the Transaction is ended, that the
fact if you call an procedure in the outer cursor which build up a cursor i
the prcedure)
Its easy to recode if you just write a simple cursor which call a procedure
and this sp establish a cursor which run into an error, try to declare the
cursor with the same name now from the QA.
HTH, Jens Suessmeyer.
http://www.sqlserver2005.de
--
"nonno" <nonno@.discussions.microsoft.com> schrieb im Newsbeitrag
news:C8DF1B00-5A10-413D-83A8-2986CBE6B4A2@.microsoft.com...
> another question:
> if I open a cursor in a transaction and the transaction rollback, will the
> cursor be automatically closed and deallocated?
> "Jens Smeyer" wrote:
>|||To add to the responses by Jens, it is often possible to use a set-based
processing rather than cursors. The set-based approach usually provides
better performance.
Hope this helps.
Dan Guzman
SQL Server MVP
"nonno" <nonno@.discussions.microsoft.com> wrote in message
news:8EFFAC69-6544-4923-8A41-F228688A2F93@.microsoft.com...
> Hi, I'm a newbie on using cursor. My question is if I create an update
> trigger that loop through the Inserted rows by a cursor, and inside the
> cursor loop, a stored procedure is executed, which contains a cursor loop
> too, any problem about it?|||But why would anyone write code like that in SQL? It is a XXXXX to
maintain, proprietary and each cursor is 1 to 2 orders of magnitude
slower than declarative SQL.
Post the DDL and a statement of the problem and you can get a better
answer.

Saturday, February 25, 2012

Can anyone explain what the pre-execute phase is doing for OLE DB data sources using sql com

I have a project which spans multiple servers and aggregates literally billions of rows of data into a much smaller and manageable result set which I store on another server. There are two stored procedures which take up 99.9% of the processing time. Each one of these SPs are estimated to run over 3 hours a piece (gives you an indication of how much data there really is). With in this SSIS package, I have two control flows, one for each SP. When I run the SSIS package from VS2005, I can see that there is a pre-execute phase which takes about an hour abnd a half to complete, and then move on to executing. My question is, what the heck is this, and what is it doing? I know it validates the sql in the procedure, but does it actually run the SP during that pre-execute phase? If it does, is there any way to get around that?

Any help would be appreciated.

Have tried to open a SQL profiler to see what is going on on the DB server during that period?

Rafael Salas

|||Yes, I have, and I did not see anything that stood to me as a procedure call to that SP. I just want to know why it takes so long to pre-execute.

Friday, February 24, 2012

Can any one explain this query behavior

db_TBOdb_TBT
----------
------------------
TBOID | Date TBTID| TBOID| Date
--------------------------
rp01 | 01/08/2006 ap01 |rp01|02/08/2006
many rows ap02 | rp01 |05/08/2006
ap03 |rp03|04/08/2006

I want to find TBTTD field of table db_TBT
who have of db_TBO date db_TBT table date
and TBOID should be 'rp01' of both tables

when I give a query as it works as I needed

Select TBT.Date
from db_TBO TBO , db_TBT TBT
where TBT.TBOID = 'rp01'
and TBO.TBOID ='rp01'
and TBO.Date TBT.Date

My doubt is when I run the following query
" Select TBT.Date from db_TBO TBO , db_TBT TBT where TBT.TBOID = 'rp01'
"
it gives me more than 7 records

when I run the query using some change
" Select TBT.Date , TBT.TBID from db_TBO TBO , db_TBT TBT where
TBT.TBOID = 'rp01' "
it gives more rows than previous row

Can u give me any explations.

Thanks
PaiOn 23 Aug 2006 22:54:21 -0700, pai wrote:

Quote:

Originally Posted by

>db_TBOdb_TBT
>----------
>------------------
>TBOID | Date TBTID| TBOID| Date
>--------------------------
>rp01 | 01/08/2006 ap01 |rp01|02/08/2006
>many rows ap02 | rp01 |05/08/2006
> ap03 |rp03|04/08/2006
>
>I want to find TBTTD field of table db_TBT
>who have of db_TBO date db_TBT table date
>and TBOID should be 'rp01' of both tables
>


(snip)

Quote:

Originally Posted by

>
>My doubt is when I run the following query
>" Select TBT.Date from db_TBO TBO , db_TBT TBT where TBT.TBOID = 'rp01'
>"
>it gives me more than 7 records


Hi Pai,

Of course it does. The FROM clause lists two tables, comma-seperated.
That means that you'll get a cross join, also known as cartesian
product, between the two tables: each row from the first table will be
paired with each row from the second table. After that (*), the WHERE
clause removes rows based on TBT.TBOID. The result set willl include
only TBT-rows with TBOID equal to 'rp01' - but each of those rows will
still be paired against each of the rows in db_TBO.

Quote:

Originally Posted by

>when I run the query using some change
>" Select TBT.Date , TBT.TBID from db_TBO TBO , db_TBT TBT where
>TBT.TBOID = 'rp01' "
>it gives more rows than previous row


I don't understand this. The only difference between this query and the
previous query is the addition of one more column in the WHERE clause.
That shoould never affect the number of rows returned. Are you sure that
you didn't make a mistake when you copied the SQL into your message?

(snipped from above:)

Quote:

Originally Posted by

>when I give a query as it works as I needed
>
>Select TBT.Date
>from db_TBO TBO , db_TBT TBT
>where TBT.TBOID = 'rp01'
>and TBO.TBOID ='rp01'
>and TBO.Date TBT.Date


Indeed. This query also starts (*) with the cross join, but then retains
only rows with both TBT.TBOID and TBO.TBOOID equal to 'rp01'. That means
that you're left with each TBT-row for 'rp01' paired to each TBO-row for
'rp01'. These results are then further filtered by the date comparison.

I think your problems arise out of the use of the "old-style" FROM
clause. The newer style, with explicit joins, makes it much harder to
make this kind of mistakes since it forces you to explicitly write down
the join criteria:

SELECT some columns
FROM db_TBO AS TBO
INNER JOIN db_TBT AS TBT
ON TBO.TBOID = TBT.TBOID
WHERE TBO.Date TBT.Date;

(*) The order of evaluation described here is only a logical explanation
of the process. The query optimizer is free to (and definitely will, in
this case) change the order of evaluation, as long as the results remain
the same.

--
Hugo Kornelis, SQL Server MVP

Can an update statement be used for interpolating missing data?

Here is a small sample of data from a table of about 500 rows
(Using MSSqlserver 2000)

EntryTime Speed Gross Net
------ -- --
21:09:13.310 0 0 0
21:09:19.370 9000 NULL NULL
21:09:21.310 NULL 95 NULL
21:10:12.380 9000 NULL NULL
21:10:24.310 NULL 253 NULL
21:11:24.370 8000 NULL NULL
21:11:27.310 NULL 410 NULL
21:11:51.320 NULL 438 NULL
21:11:51.490 NULL NULL 10

After the first row, every row has only one value of the three.
I would like to replace all the NULL values with calculated
interpolations.

I can do it w/ cursors or while loops.
I could do it w/ VB (I think)

Can this be done w/ an Update statement using self joins?
What would be the best way?

The value for speed can increase or decrease over time, but can never
be < 0

Net is always less than gross, and neither can go below 0.

TIA for any helpful suggestions.

Thanks,
BMIt's not really clear how you want to calculate the new values, but
perhaps you can look at CASE and COALESCE in Books Online. If this
doesn't help, then you should post some more information about how you
want to calculate the new values.

Simon|||I expect it will be possible with an UPDATE and a join/subquery.

UPDATE YourTable
SET speed =
(SELECT ...
FROM YourTable
WHERE entrytime < YourTable.entrytime ...)
WHERE speed IS NULL

If you need a complete solution then explain the calculation, show the
result you want and post DDL for the table. Also, it's best to post sample
data as INSERT statements so that others can more easily test out possible
solutions. That way you'll get accurate and useful answers more quickly.
See:
http://www.aspfaq.com/etiquette.asp?id=5006

--
David Portas
SQL Server MVP
--|||Thank you greatly for the FAQ link. I learned a lot just reading it.

The table:
if exists (select * from dbo.sysobjects where id =
object_id(N'[tblProfileTemp]') and OBJECTPROPERTY(id, N'IsUserTable') =
1)
drop table [tblProfileTemp]
GO

if not exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[tblProfileTemp]') and OBJECTPROPERTY(id,
N'IsUserTable') = 1)
BEGIN
CREATE TABLE [tblProfileTempX] (
--[Item] [int] IDENTITY (1, 1) NOT NULL ,
[Item] [int] NOT NULL ,
[EntryTime] [datetime] NULL ,
[RunTime] [numeric](12, 3) NULL ,
[Speed] [int] NULL ,
[gross] [int] NULL ,
[net] [int] NULL
) ON [PRIMARY]
END

The insert statements (the code to generate this was an education in
itself).
(10 rows should be enough) I commented out the identity contraint so
inserting w/o the column list would be possible.
INSERT INTO [tblProfileTempx] VALUES(1,'Jun 21 2005
9:09:13:310PM',0.000,0,0,0)
INSERT INTO [tblProfileTempx] VALUES(2,'Jun 21 2005
9:09:19:370PM',6.060,9000,NULL,NULL)
INSERT INTO [tblProfileTempx] VALUES(3,'Jun 21 2005
9:09:21:310PM',8.000,NULL,95,NULL)
INSERT INTO [tblProfileTempx] VALUES(4,'Jun 21 2005
9:10:12:380PM',59.070,9000,NULL,NULL)
INSERT INTO [tblProfileTempx] VALUES(5,'Jun 21 2005
9:10:24:310PM',71.000,NULL,253,NULL)
INSERT INTO [tblProfileTempx] VALUES(6,'Jun 21 2005
9:11:24:370PM',131.060,8000,NULL,NULL)
INSERT INTO [tblProfileTempx] VALUES(7,'Jun 21 2005
9:11:27:310PM',134.000,NULL,410,NULL)
INSERT INTO [tblProfileTempx] VALUES(8,'Jun 21 2005
9:11:51:320PM',158.010,NULL,438,NULL)
INSERT INTO [tblProfileTempx] VALUES(9,'Jun 21 2005
9:11:51:490PM',158.180,0,NULL,NULL)
INSERT INTO [tblProfileTempx] VALUES(10,'Jun 21 2005
9:13:51:310PM',278.000,NULL,446,NULL)

Explanation of data:
The data represents the output of a running press. Each data element
is recorded at EntryTime. RunTime represents the time elapsed since
the start, and is expressed in seconds. Gross is number of copies
printed. Net is number of copies not rejected automatically by various
defect detectors.

Desired Result:
Example:
Gross for item 1 is 0
Gross for item 2 is null
Gross for item 3 is 95

I need to replace the null in item 2 with a value that represents the
gross count for that time, assuming a constant press speed. It will
not necesarily be constant, but the error will be slight.

The formula for that value will be:

Gross2 = Gross1 + ((Gross3 - Gross1) * ((RunTime2-RunTime1) / (RunTime3
- RunTime1)))

Similar interpolations will be calculated for Net and Speed.

It gets harder where there are two or more nulls between known values.

I was working along the lines of:

Update t1
Set t1.Gross = t0.Gross + ((t2.Gross = t0.Gross) *
((t1.runtime-t0.runtime)/(t2.runtime-t0.runtime)))

from tblProfileTempX t1 inner join tblProfileTempX t0 on t0.item =
t1.item
inner join tblProfileTempX t2 on t2.item = t0.item

where t1.gross is null
and t0.EntryTime = (select Max(EntryTime) from tblProfileTempX
where gross is not null and item < t1.item)
and t2.EntryTime = (select Min(EntryTime) from tblProfileTempX
where gross is not null and item > t1.item)

I've reduce the errors to the following:
Server: Msg 170, Level 15, State 1, Line 2
Line 2: Incorrect syntax near '='.
Server: Msg 156, Level 15, State 1, Line 9
Incorrect syntax near the keyword 'and'.

Thanks in advance for your time and effort, and apologies for the group
etiquette breach,

Regards,
BM

David Portas wrote:
> I expect it will be possible with an UPDATE and a join/subquery.
> UPDATE YourTable
> SET speed =
> (SELECT ...
> FROM YourTable
> WHERE entrytime < YourTable.entrytime ...)
> WHERE speed IS NULL
> If you need a complete solution then explain the calculation, show the
> result you want and post DDL for the table. Also, it's best to post sample
> data as INSERT statements so that others can more easily test out possible
> solutions. That way you'll get accurate and useful answers more quickly.
> See:
> http://www.aspfaq.com/etiquette.asp?id=5006
> --
> David Portas
> SQL Server MVP
> --|||Groan:
Tired eyes mistook an = sign for a - sign in line two.
With that fixed, it runs, but:

0 rwos affected|||[posted and mailed, please reply in news]

Vorpal (brumac@.gmail.com) writes:
> Here is a small sample of data from a table of about 500 rows
> (Using MSSqlserver 2000)
> EntryTime Speed Gross Net
> ------ -- --
> 21:09:13.310 0 0 0
> 21:09:19.370 9000 NULL NULL
> 21:09:21.310 NULL 95 NULL
> 21:10:12.380 9000 NULL NULL
> 21:10:24.310 NULL 253 NULL
> 21:11:24.370 8000 NULL NULL
> 21:11:27.310 NULL 410 NULL
> 21:11:51.320 NULL 438 NULL
> 21:11:51.490 NULL NULL 10
> After the first row, every row has only one value of the three.
> I would like to replace all the NULL values with calculated
> interpolations.
> I can do it w/ cursors or while loops.
> I could do it w/ VB (I think)
> Can this be done w/ an Update statement using self joins?

Not "an", but a couple. In the below script I get the data into a temp
table with an IDENTITY column, which has a consecutive number. I then
find the next and previous row with a non-NULL value for speed, for those
rows that have a NULL value. Once I have these pointers I can make the
interpolation. There is no extrapolation for the NULL values at the end.

The number of UPDATE statements could be reduced if you have three
sets of pointer columns, but I'm not sure that is worth the pain.

The script does not include handling of Net. That is left as an exercise
to the reader. :-)

CREATE TABLE tbl (entrytime datetime NOT NULL PRIMARY KEY,
speed int NULL,
gross int NULL,
net int NULL)
go
INSERT tbl(entrytime, speed, gross, net)
SELECT '21:09:13.310', 0, 0, 0 UNION
SELECT '21:09:19.370', 9000, NULL, NULL UNION
SELECT '21:09:21.310', NULL, 95, NULL UNION
SELECT '21:10:12.380', 9000, NULL, NULL UNION
SELECT '21:10:24.310', NULL, 253, NULL UNION
SELECT '21:11:24.370', 8000, NULL, NULL UNION
SELECT '21:11:27.310', NULL, 410, NULL UNION
SELECT '21:11:51.320', NULL, 438, NULL UNION
SELECT '21:11:51.490', NULL, NULL, 10
go
CREATE TABLE #temp (ident int IDENTITY UNIQUE,
entrytime datetime NOT NULL PRIMARY KEY,
speed int NULL,
gross int NULL,
net int NULL,
prevval int NULL,
nextval int NULL)

INSERT #temp(entrytime, speed, gross, net)
SELECT entrytime, speed, gross, net
FROM tbl
ORDER BY entrytime

UPDATE #temp
SET prevval = (SELECT MAX(t2.ident)
FROM #temp t2
WHERE t2.ident < t.ident
AND t2.speed IS NOT NULL)
FROM #temp t
WHERE t.speed IS NULL

UPDATE #temp
SET nextval = (SELECT MIN(t2.ident)
FROM #temp t2
WHERE t2.ident > t.ident
AND t2.speed IS NOT NULL)
FROM #temp t
WHERE t.speed IS NULL

UPDATE t
SET speed = p.speed +
1E0 * (n.speed - p.speed) * (t.ident - t.prevval) /
(t.nextval - t.prevval)
FROM #temp t
JOIN #temp p ON t.prevval = p.ident
JOIN #temp n ON t.nextval = n.ident
WHERE t.speed IS NULL

UPDATE #temp
SET prevval = NULL, nextval = NULL

UPDATE #temp
SET prevval = (SELECT MAX(t2.ident)
FROM #temp t2
WHERE t2.ident < t.ident
AND t2.gross IS NOT NULL)
FROM #temp t
WHERE t.gross IS NULL

UPDATE #temp
SET nextval = (SELECT MIN(t2.ident)
FROM #temp t2
WHERE t2.ident > t.ident
AND t2.gross IS NOT NULL)
FROM #temp t
WHERE t.gross IS NULL

UPDATE t
SET gross = p.gross +
1E0 * (n.gross - p.gross) * (t.ident - t.prevval) /
(t.nextval - t.prevval)
FROM #temp t
JOIN #temp p ON t.prevval = p.ident
JOIN #temp n ON t.nextval = n.ident
WHERE t.gross IS NULL

UPDATE #temp
SET prevval = NULL, nextval = NULL

go
UPDATE tbl
SET speed = t.speed,
gross = t.gross,
net = t.net
FROM tbl
JOIN #temp t ON tbl.entrytime = t.entrytime
go
SELECT * FROM #temp
SELECT * FROM tbl ORDER BY entrytime
go
DROP TABLE tbl
DROP TABLE #temp

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Erland Sommarskog (esquel@.sommarskog.se) writes:
> UPDATE t
> SET speed = p.speed +
> 1E0 * (n.speed - p.speed) * (t.ident - t.prevval) /
> (t.nextval - t.prevval)
> FROM #temp t
> JOIN #temp p ON t.prevval = p.ident
> JOIN #temp n ON t.nextval = n.ident
> WHERE t.speed IS NULL

So I did not consider time. This might be better:

UPDATE t
SET speed = p.speed +
1E0 * (n.speed - p.speed) *
datediff(ms, p.entrytime, t.entrytime) /
datediff(ms, p.entrytime, n.entrytime)
FROM #temp t
JOIN #temp p ON t.prevval = p.ident
JOIN #temp n ON t.nextval = n.ident
WHERE t.speed IS NULL

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||I have settled on the following for now:
1. First, if the final value is a null, set it to the maximum value
for that field.
This is necessary so that the intervening values can be calculated.
This update does that:
update t1
set Gross = t0.gross
from tblProfiletemp t1, tblprofiletemp t0
where t1.gross is null
and t1.entrytime = (select max(entrytime) from tblprofiletemp)
and t0.gross = (select max(gross) from tblprofiletemp)

Once that is done, then the following update statement fills in all
intervening values with the correct value:
Update t1
set Gross=t0.Gross + ((t2.Gross - t0.Gross) *
((t1.runtime-t0.runtime)/(t2.runtime-t0.runtime)))
from tblProfiletemp t0 ,tblProfiletemp t1,tblProfiletemp t2
where t1.gross is null
and t0.entrytime = (select Max(EntryTime) from tblProfiletemp where
item < t1.item and gross is not null)
and t2.EntryTime = (select Min(EntryTime) from tblProfiletemp where
item > t1.item and gross is not null)

The reason I could not get the update statement to work before is that
I was erroneously creating self joins.

The above will work properly on the columns where the data always
increases. It may need some modification for the speed columns.

Thanks for all the input.
BM|||Don't rely on the IDENTITY column to drive the sequence. IDENTITY is
only supposed to be an arbitrary key. EntryTime should be a better way
to do it:

UPDATE tblProfileTemp
SET gross =
(SELECT T0.gross +
((T2.gross - T0.gross) *
((tblProfileTemp.runtime-T0.runtime)/(T2.runtime-T0.runtime)))
FROM tblProfileTemp AS T0,
tblProfileTemp AS T2
WHERE T0.entrytime =
(SELECT MAX(entrytime)
FROM tblProfileTemp AS T
WHERE entrytime < tblProfileTemp.entrytime
AND gross IS NOT NULL)
AND T2.entrytime =
(SELECT MIN(entrytime)
FROM tblProfileTemp AS T
WHERE entrytime > tblProfileTemp.entrytime
AND gross IS NOT NULL))
WHERE gross IS NULL

Thanks for posting the DDL and sample. It helped.

--
David Portas
SQL Server MVP
--|||For the speed table, I took a different approach.
Once the values for gross copy count have been inserted, then the speed
can be calculated backwards as
Speed = (Gross - Gross0)/(RunTime - RunTime0).

I changed the runtime so it is recorded in seconds, rather than
minutes, and round the speed value to the neares 100.
These two changes give a smoother graph.

Where a speed needs to be calculated from two gross values recorded
very short times apart, then the speed sometimes appears anomalously
high or low. (usually high).

The results of this are now in testing, and I'll see what feedback from
the users is before making further changes.

Thanks tremendously to all who helped.

Tuesday, February 14, 2012

Can a Data Flow Task mimic Bulk Insert?

Hi Guys,

The way I understand a Data Flow Task is that it inserts the rows from the source to destination one by one. Is there a way to make it act like a bulk insert task? We have been experiencing performance issues when inserting a lot of rows from one table to another. If there's no way to actually do it, can a bulk insert task functionality be scripted? Coz what I need is a table to table insert, and the bulk insert task only accepts data files as sources.

Thanks!

Kervy

What destination object are you using? The OLEDB Destination and the SQL Server destination both enable bulk insert options for SQL Server.

How many rows are you inserting?

Donald

|||

Oh you mean to say, by default, an OLEDB source inserts to an OLEDB Destination in bulk? We are only inserting a couple of hundred thousand rows, but the source table is in the US. destinations are in Europe and Asia Pacific, so we need to optimize this as much as we can given the proximity of the servers.

We are using OLEDB source and OLEDB Destination

Thanks

|||

If you need to performance tune your data-flow then you should read a whitepaper that I have mentioned here: http://blogs.conchango.com/jamiethomson/archive/2006/04/09/3594.aspx

There's also more useful information here: http://blogs.conchango.com/jamiethomson/archive/2005/04/20/1319.aspx

Note that SSIS does not insert records one at a time - that would be a performance killer. The SSIS pipeline processes data in buffers and the contents of each buffer is inserted at the same time! A large part of performance tuning your application is finding the optimum buffer size.

Note that in your scenario the bottleneck is likely to be the georgraphic disparity of the source and destination, not how fast SSIS can process it. Try and reduce the amount of data that is coming over the wire if possible - i.e. Filter the data at source and only select columns that you require.

-Jamie

|||

hi Jamie,

Thanks for that response. I will read more on how to performance tune my data flow. One accepted cause for the horrible execution time is the distance between servers (destinations are in 2 different continents!), that is why we are trying to figure out if there are other ways to improve on this. We tried testing with a destination server in the same area, and it took about 9 minutes.. compared to the 16 hours it took when the target was Asia Pacific.. we may have to accept that performance issue for now..

total number of rows to be inserted = 200,000

total number of columns = 167

|||

KervyChoa wrote:

hi Jamie,

Thanks for that response. I will read more on how to performance tune my data flow. One accepted cause for the horrible execution time is the distance between servers (destinations are in 2 different continents!), that is why we are trying to figure out if there are other ways to improve on this. We tried testing with a destination server in the same area, and it took about 9 minutes.. compared to the 16 hours it took when the target was Asia Pacific.. we may have to accept that performance issue for now..

total number of rows to be inserted = 200,000

total number of columns = 167

Exactly, hence my previous comment "Try and reduce the amount of data that is coming over the wire if possible - i.e. Filter the data at source and only select columns that you require."

The point here is to reduce the amount of data coming over the wire because that is quite obviously the bottleneck in your scenario.

-Jamie

|||I tried using perfmon to see what can be done, but I can't find the SQL Server: SSIS Pipeline Performance object in perfmon.. Am I missing something here?|||

KervyChoa wrote:

I tried using perfmon to see what can be done, but I can't find the SQL Server: SSIS Pipeline Performance object in perfmon.. Am I missing something here?

As we have already determined (twice) the bottleneck is not within the SSIS pipeline so looking at this perf counter is not going to help you.

Unless you pick one of the machines up and move it to a different continent, or improve the speed of the link between them, you're not going to see any eprformance improvement.

-Jamie

Sunday, February 12, 2012

Can @@ROWCOUNT = 0 even when a row is inserted into a table?

Does @.@.ROWCOUNT always return an accurate acount of rows affected by last query OR can it be equal to zero when some rows have been affected?

The @.@.ROWCOUNT function returns the number of rows affected by the last statement, no matter what type (insert/update/delete/select) of the statement is. It just like if you execute a statement in Query Analyzer with the NOCOUNT option off, a message will come with result indicates how many rows are affected.|||And to be clear, the availability of @.@.ROWCOUNT is not affected by the SET NOCOUNT setting.

Don