Showing posts with label records. Show all posts
Showing posts with label records. Show all posts

Tuesday, March 20, 2012

quicker way to create indexes

Hi,

I have a new job. It needs to drop and re-create (by insert) a table
every night. The table contains approximately 3,000,000 (and growing)
records. The insert is fine, runs in 2 minutes. The problem is that
when I create the indexes on the table, it is taking 15-20 minutes.
There is one clustered index and 11 non-clustered. This is a lookup
table that takes many different paremeters, so it really needs the
indexes for the user interface to run efficiently. However, the
database owners aren't keen on a job taking 20 minutes to run every
night.

Any ideas?shelleybobelly (shelleybobelly@.yahoo.com) writes:

Quote:

Originally Posted by

I have a new job. It needs to drop and re-create (by insert) a table
every night. The table contains approximately 3,000,000 (and growing)
records. The insert is fine, runs in 2 minutes. The problem is that
when I create the indexes on the table, it is taking 15-20 minutes.
There is one clustered index and 11 non-clustered. This is a lookup
table that takes many different paremeters, so it really needs the
indexes for the user interface to run efficiently. However, the
database owners aren't keen on a job taking 20 minutes to run every
night.


Without knowing much about the data, it's difficult to tell. But I find it
difficult to believe that data changes that much in a lookup table. Then
again, I would not expect a look-up table to have three million rows.
Anyway, rather than dropping and recreating, maybe it's more effective
to load into staging table, and then update changed rows, insert new
ones, and delete old ones.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Right, I agree. If I run it the other way (just update the big lookup
table from the separate tables where there were changes), it runs much
quicker, only deleting/adding a thousand or so records a day. The
owners of this DB are doing things differently, but I guess I'll just
have to 'educate' them on this one. Then I'll create another job to
re-index this table about once a week or so to keep it from
fragmenting.

BTW, this is for a Legal department that has to look up files that can
be up to 20 years old, but they don't have all the information. That's
why the 3 million records in a lookup table. It is a subset of a HUGE
archive table that contains 255 columns, too.

Thanks Erland, keep up your postings.

Erland Sommarskog wrote:

Quote:

Originally Posted by

shelleybobelly (shelleybobelly@.yahoo.com) writes:

Quote:

Originally Posted by

I have a new job. It needs to drop and re-create (by insert) a table
every night. The table contains approximately 3,000,000 (and growing)
records. The insert is fine, runs in 2 minutes. The problem is that
when I create the indexes on the table, it is taking 15-20 minutes.
There is one clustered index and 11 non-clustered. This is a lookup
table that takes many different paremeters, so it really needs the
indexes for the user interface to run efficiently. However, the
database owners aren't keen on a job taking 20 minutes to run every
night.


>
Without knowing much about the data, it's difficult to tell. But I find it
difficult to believe that data changes that much in a lookup table. Then
again, I would not expect a look-up table to have three million rows.
Anyway, rather than dropping and recreating, maybe it's more effective
to load into staging table, and then update changed rows, insert new
ones, and delete old ones.
>
>
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
>
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

|||Not starting over every night would certainly be the best approach,
but anyway...

One thing you might try is to leave the clustered index on the table,
and order the data by the clustering key before loading. It is
possible that the longer time to load with the index in place will be
more than offset by the time saved in creating the clustered index.

Roy Harvey
Beacon Falls, CT

On 18 Jul 2006 13:58:55 -0700, "shelleybobelly"
<shelleybobelly@.yahoo.comwrote:

Quote:

Originally Posted by

>Hi,
>
>I have a new job. It needs to drop and re-create (by insert) a table
>every night. The table contains approximately 3,000,000 (and growing)
>records. The insert is fine, runs in 2 minutes. The problem is that
>when I create the indexes on the table, it is taking 15-20 minutes.
>There is one clustered index and 11 non-clustered. This is a lookup
>table that takes many different paremeters, so it really needs the
>indexes for the user interface to run efficiently. However, the
>database owners aren't keen on a job taking 20 minutes to run every
>night.
>
>Any ideas?

|||"shelleybobelly" <shelleybobelly@.yahoo.comwrote in message
news:1153260795.628994.213350@.h48g2000cwc.googlegr oups.com...

Quote:

Originally Posted by

Right, I agree. If I run it the other way (just update the big lookup
table from the separate tables where there were changes), it runs much
quicker, only deleting/adding a thousand or so records a day. The
owners of this DB are doing things differently, but I guess I'll just
have to 'educate' them on this one. Then I'll create another job to
re-index this table about once a week or so to keep it from
fragmenting.
>
BTW, this is for a Legal department that has to look up files that can
be up to 20 years old, but they don't have all the information. That's
why the 3 million records in a lookup table. It is a subset of a HUGE
archive table that contains 255 columns, too.
>


To suggestions:

1) move the indexes to an NDF file on a separate set of physical disks.

This may help if you're disk I/O bound at all.

2) May want to consider using full-text indexing for some of this.

Quote:

Originally Posted by

Thanks Erland, keep up your postings.
>
>
Erland Sommarskog wrote:

Quote:

Originally Posted by

shelleybobelly (shelleybobelly@.yahoo.com) writes:

Quote:

Originally Posted by

I have a new job. It needs to drop and re-create (by insert) a table
every night. The table contains approximately 3,000,000 (and growing)
records. The insert is fine, runs in 2 minutes. The problem is that
when I create the indexes on the table, it is taking 15-20 minutes.
There is one clustered index and 11 non-clustered. This is a lookup
table that takes many different paremeters, so it really needs the
indexes for the user interface to run efficiently. However, the
database owners aren't keen on a job taking 20 minutes to run every
night.


Without knowing much about the data, it's difficult to tell. But I find


it

Quote:

Originally Posted by

Quote:

Originally Posted by

difficult to believe that data changes that much in a lookup table. Then
again, I would not expect a look-up table to have three million rows.
Anyway, rather than dropping and recreating, maybe it's more effective
to load into staging table, and then update changed rows, insert new
ones, and delete old ones.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at


http://www.microsoft.com/technet/pr...oads/books.mspx

Quote:

Originally Posted by

Quote:

Originally Posted by

Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx


>

|||On 18 Jul 2006 13:58:55 -0700, "shelleybobelly"
<shelleybobelly@.yahoo.comwrote:

Quote:

Originally Posted by

>the database owners aren't keen on a job taking 20 minutes to run
>every night.


Is it automated? Are there users doing regular work at night? If
the answers are "yes" and "no" respectively, then why do they care
how long it runs?

quicker way of writing a LIKE query

Hi

I have two product tables in two different databases, both contain thousands of records. I have to write a query that suggests matches on similar codes, and have come up with:

SELECT TB1.product, TB2.product
FROM TB1
JOIN (select distinct product
from db2.dbo.TB2) as TB2 --this table has PK of product and warehouse
ON TB2.product LIKE '%' + TB1.product+'%'

which DOES work, but because the table have many rows,takes time to do it... is there a way of rewritting this query, so it gives a faster result?

Thanks in advance...The problem that I see is that you are using a definition that requires a table scan for TB2 in order to determine row-by-row if the value of TB1.product exists anywhere in TB2.product.

This type of search is an ugly process to implement using just a set based language like SQL. This kind of problem is why Full Text Search (http://msdn2.microsoft.com/en-us/library/ms142571.aspx) was added to MS-SQL. Beware, in that Full Text Search is definitely NOT a "free lunch", there is definitely an overhead cost associated with it.

There are other ways to speed up the process, but none of them are very pretty. My first thought is to evaluate the cost/benefit of using Full Text Search, and only to pursue other answers if you decide not to use it and really need something else.

-PatP|||i would like to see the WHERE clause using full text, please

WHERE CONTAINS( ... ??

your guidance here, pat, will, as usual, be deeply appreciated|||I'd like to see some sample data, with further clarification on what he considers a partial match.|||who said partial match?

here's some sample data showing columns which match
TB1.product TB2.product
shampoo Kerastase Resistance Bain Volumactive Shampoo Volumizing
philosophy cinnamon buns shampoo, conditioner, & shower gel
H2O Plus Sea Marine Revitalizing Shampoo
shaving Proraso Eucalyptus & Menthol Shaving Cream 150 ml.
The Art of Shaving Unscented Pre-Shave Oil
Tweezerman Badger Hair Shaving Brush|||who said partial match?LIKE implies partial matches, whether he wishes or not. And where did you get his data, or did I miss a smiley somewhere?|||And where did you get his datai made it up

his first post said that his query works

this data fits that query

are you smiley-deprived? here, have a few: :) ;) :blush: :rolleyes:|||Thanks. I needed those.|||i would like to see the WHERE clause using full text, pleaseI know... As you are fond of reminding me, you are so NOT a DBA. This one falls outside of the scope of solutions in which you like to play, it is one of those tasks where you just get the job done and move on with life.

The CONTAINS function doesn't work the way you are implying, I don't know of a completely set-based solution for this kind of problem. I would retrieve the rows from the smaller table to a client (such as VBA within a DTS package), and build a temp table of the matches or partial matches so that I could return that. You could also do it with a cursor and dynamic SQL, but that strikes me as even uglier. This is ugly, but it will perform better than the "brute force" of the LIKE approach.

-PatP|||hey

Thanks for all the replies...

I had advanced a wee bit...
Basically I have been able to cut down the amount of rows in TB1 on some factors, and dumped it into a temp table...

I will look into the Full Text Search : )

Thanks again

Quick\Dirty Editing of SQL-SERVER Records on Web

I am looking for a tool which will quickly allow me to list records in a
database and edit them via a Web Interface. Nothing fancy, just list, edit
and save.
Thanks!
You might want to give ASP.NET a look -- www.asp.net.com.
--Mary
On Tue, 13 Jul 2004 09:26:01 -0700, "MEI" <MEI@.MEI.COM> wrote:

>I am looking for a tool which will quickly allow me to list records in a
>database and edit them via a Web Interface. Nothing fancy, just list, edit
>and save.
>Thanks!
>

Quick\Dirty Editing of SQL-SERVER Records on Web

I am looking for a tool which will quickly allow me to list records in a
database and edit them via a Web Interface. Nothing fancy, just list, edit
and save.
Thanks!You might want to give ASP.NET a look -- www.asp.net.com.
--Mary
On Tue, 13 Jul 2004 09:26:01 -0700, "MEI" <MEI@.MEI.COM> wrote:

>I am looking for a tool which will quickly allow me to list records in a
>database and edit them via a Web Interface. Nothing fancy, just list, edit
>and save.
>Thanks!
>

Quick\Dirty Editing of SQL-SERVER Records on Web

I am looking for a tool which will quickly allow me to list records in a
database and edit them via a Web Interface. Nothing fancy, just list, edit
and save.
Thanks!You might want to give ASP.NET a look -- www.asp.net.com.
--Mary
On Tue, 13 Jul 2004 09:26:01 -0700, "MEI" <MEI@.MEI.COM> wrote:
>I am looking for a tool which will quickly allow me to list records in a
>database and edit them via a Web Interface. Nothing fancy, just list, edit
>and save.
>Thanks!
>

Monday, March 12, 2012

Quick SQL Question

Hey,

I know thsi is a silly question but gonna ask it anyway :)

When you are using the 'insert' statement to insert records in to a database are all of the fields in the db table required for a successfull adding of a record.

Just that i have 16 fields in my db table and want to insert only 11 fields in to the new record and it is giving me an error, and i am sure my SQL is correct

So are all fields required ?

Thankyou

ChrisHey,

i have realised that the password field i am passing to the database causes the error, if i take this out, all values are added to the db. i have tried adding this field using a stored procedure but still causes the 'There is an error in your INSERT Statement" error.

Must i convert my password to some other data type before i add it?

I have out put my sql statement to a label and it sees the password fine but just doesnt add it, this is puzzeling me as it just a textbox and all the other text boxes add fine, i have checked the names of my control and database fields and all are correct.

Please help

chris|||Probelm Solved

Cheers anyway :)

Wednesday, March 7, 2012

Quick question

One quick question...I have a MedicalCause table and it contains 12
billions of records and I wanna do partitioning that table and wanna create
view as well as index on that view but the problem is the that SQL server
doesn't allow to create an index on view which contain "union" and I have to
go for partitioning then obviously I will use Union all query... any idea
what I have to do to fulfuil to create view as well as index over it.
Hi
Have you read through
http://msdn.microsoft.com/library/de...es_06_17zr.asp
to make sure that you have met all the correct criteria?
John
"Joh" <joh@.mailcity.com> wrote in message
news:O8C8IitYFHA.2288@.TK2MSFTNGP14.phx.gbl...
> One quick question...I have a MedicalCause table and it contains 12
> billions of records and I wanna do partitioning that table and wanna
> create
> view as well as index on that view but the problem is the that SQL server
> doesn't allow to create an index on view which contain "union" and I have
> to
> go for partitioning then obviously I will use Union all query... any idea
> what I have to do to fulfuil to create view as well as index over it.
>

Quick Method to delete from Two Tables

I have two tables ( a & b ) Both are linked by a ledgerref field. table what
would be the quickest and easiest way to delete records from both when
a.textStatus = 1the only way.. the usual way
delete from b from a,b
where b.ledgerref = a.ledgerref
and a.textStatus = 1
delete from a where textStatus = 1
-Omnibuzz (The SQL GC)
http://omnibuzz-sql.blogspot.com/|||and of course enclose it with a transaction :)
--
-Omnibuzz (The SQL GC)
http://omnibuzz-sql.blogspot.com/|||If the two tables are PK-FK linked, you could use CASCADE DELETE.
Arnie Rowland, YACE*
"To be successful, your heart must accompany your knowledge."
*Yet Another certification Exam
"Peter Newman" <PeterNewman@.discussions.microsoft.com> wrote in message
news:1523DBF8-B27F-4351-970C-AF93A4316705@.microsoft.com...
>I have two tables ( a & b ) Both are linked by a ledgerref field. table
>what
> would be the quickest and easiest way to delete records from both when
> a.textStatus = 1|||Or getting it out of dialect, and correcting the "textStatus" data
element name (test and status are both suffixes to an attribute in
ISO-11179). I will not comment on the practice of using flags in SQL
to mimic an assembly language programming, or redundant tables to mimic
scratch tapes.
DELETE FROM Beta
WHERE EXISTS
(SELECT *
FROM Alpha
WHERE Beta.ledger_ref = Alpha.ledger_ref
AND Alpha.foobar_status = 1);
DRI action would be better. The best solution would be a proper
relational design.

Monday, February 20, 2012

Questions on use of SQL Server 2005 functionality in Gridview Paging

I have a webpage that displays 4000 or more records in a GridView control powered by a SqlDataSource. It's very slow. I'm reading the following article on custom paging: http://aspnet.4guysfromrolla.com/articles/031506-1.aspx. This article uses an ObjectDataSource, and some functionality new to Sql Server 2005 to implement custom paging.

There is a stored procedure called GetEmployeesSubestByDepartmentIDSorted that looks like this:

ALTER PROCEDURE dbo.GetEmployeesSubsetByDepartmentIDSorted
(
@.DepartmentID int,
@.sortExpression nvarchar(50),
@.startRowIndex int,
@.maximumRows int
)
AS
IF @.DepartmentID IS NULL
-- If @.DepartmentID is null, then we want to get all employees
EXEC dbo.GetEmployeesSubsetSorted @.sortExpression, @.startRowIndex, @.maximumRows
ELSE
BEGIN
-- Otherwise we want to get just those employees in the specified department
IF LEN(@.sortExpression) = 0
SET @.sortExpression = 'EmployeeID'

-- Since @.startRowIndex is zero-based in the data Web control, but one-based w/ROW_NUMBER(), increment
SET @.startRowIndex = @.startRowIndex + 1

-- Issue query
DECLARE @.sql nvarchar(4000)
SET @.sql = 'SELECT EmployeeID, LastName, FirstName, DepartmentID, Salary,
HireDate, DepartmentName
FROM
(SELECT EmployeeID, LastName, FirstName, e.DepartmentID, Salary,
HireDate, d.Name as DepartmentName,
ROW_NUMBER() OVER(ORDER BY ' + @.sortExpression + ') as RowNum
FROM Employees e
INNER JOIN Departments d ON
e.DepartmentID = d.DepartmentID
WHERE e.DepartmentID = ' + CONVERT(nvarchar(10), @.DepartmentID) + '
) as EmpInfo

WHERE RowNum BETWEEN ' + CONVERT(nvarchar(10), @.startRowIndex) +
' AND (' + CONVERT(nvarchar(10), @.startRowIndex) + ' + '
+ CONVERT(nvarchar(10), @.maximumRows) + ') - 1'

-- Execute the SQL query
EXEC sp_executesql @.sql
END

The part that's bold is the part I don't understand. Can someone shed some light on this for me? What is this doing and why?

Diane

"ROW_NUMBER() OVER(ORDER BY ' + @.sortExpression + ') as RowNum" part is generating the row number by sorting the data on sortExpression and fetching the data from employee and department. It also doing the paging on the backend side based on the row#

Check this for easy understanding:http://www.davidhayden.com/blog/dave/archive/2005/12/30/2652.aspx

|||

Thank you, I have a better understanding of this, but I'm still not sure how to use it. I want to return all the fields in the table. Will this work?

SET @.sql = 'SELECT <full fields list>

FROM (SELECT <full field list>, ROW_NUMBER() OVER(ORDER BY ' + @.sortExpression + ') as RowNum FROM <table> WHERE <condition>)

as tblinfo

WHERE RowNum BETWEEN ' + CONVERT(nvarchar(10), @.startRowIndex) + ' AND (' + CONVERT(nvarchar(10), @.startRowIndex) + ' + ' + CONVERT(nvarchar(10), @.maximumRows) + ') - 1'

Is this creating a table in a table? The article didn't explain why there's a select statement inside the from?

Diane

|||

This is the subquery. There are # of aricle on the same subject.

http://www.aspfree.com/c/a/MS-SQL-Server/Subqueries-and-Query-Expressions/

|||

Thank you. I got the query and sub query to work. Now my problem is that it will return the expected data when I preview it, but the gridview shows no data.

Diane