Showing posts with label tables. Show all posts
Showing posts with label tables. Show all posts

Thursday, March 29, 2012

Backing up SQL Server 2005 Express

How can I back up existing SQL Server 2005 Express tables? I'm presently developing on a 4 year old laptop which is toooo sloooow and need to transfer the tables and data to a desktop system.

Copy the MyDatabase.mdf and MyDatabase.ldf files to the new system.

In the new system, use Attach... from SQL studio, to connect to the copied database.

Tuesday, March 27, 2012

Backing up SP's and Functions...

Hello,

I want to write a batch file which gets the backup of the SP's,Functions,Views and Tables which belongs to me.Is there a utility to do this?

The best option may be using one of the excellent third party products. Or, try these scripts and see if you can convert one of them for your usage.

DDL -Script Database to File
http://www.wardyit.com/blog/blog/archive/2006/07/21/133.aspx
http://www.sqlteam.com/publish/scriptio
http://www.aspfaq.com/etiquette.asp?id=5006
http://www.codeproject.com/dotnet/ScriptDatabase.asp
http://www.nigelrivett.net/DMO/DMOScriptAllDatabases.html
http://rac4sql.net/qalite_main.asp

I'd recommend one of the third party products, such as SQL Script from www.ApexSQL.com.

sql

Sunday, March 25, 2012

backing up everything to sql script

is there a way to back up the entire database, data, relations, tables, and
procedures to an sql script? and if so how do i do it? thanksBrian,
> is there a way to back up the entire database, data, relations,
> tables, and procedures to an sql script? and if so how do i do
> it? thanks
Yes, in Enterprise Manager. Right-click on the database, select All
Tasks | Generate SQL Script... from the menu.
Linda|||Brian,
Just one oversite - This does not get you the DATA.
You can back up the data using:
1) A full database backup - also gets you everything else
2) Detach database and file copy mdf, ndf, ldf files (or just stop SQL server rather than detach)
3) bcp
4) dts
5) Access import via odbc ( okay not so easy to get back, but still useful )
With 3) to 5) backup is on a table by table basis.
Regards
AJ
"lindawie" <lindawie@.my-deja.com> wrote in message news:OYUXTpEkDHA.976@.tk2msftngp13.phx.gbl...
> Brian,
> > is there a way to back up the entire database, data, relations,
> > tables, and procedures to an sql script? and if so how do i do
> > it? thanks
> Yes, in Enterprise Manager. Right-click on the database, select All
> Tasks | Generate SQL Script... from the menu.
> Linda
>

Thursday, March 22, 2012

Backing up a schema

Is it possible to only backup the tables that belong to a schema?

Not with our backup utility.

You could use bcp or some other "export" tool.

Saturday, February 25, 2012

Avoiding transaction logging - at least reducing it

I have an application that uses work tables within an SP to generate a recordset output to the caller as the result of a search operation. The SP does not update the main database, and the results, although sizeable, are not saved.

Clearly I do not need to log the updates to the work tables. They start out empty and the contents could be discarded.

Does SQL Server recognise this and not log the updates to work tables? Is there any way I can indicate that a table is not significant and need not be logged at all?

Would it help to put the work tables in a separate database?

Maybe it would not make much difference anyway?

Thanks in advance for any helpFortunately (in a consistancy point of view), any update is logged, in user tables, system tables, worktables or tables in tempdb.

A specific traceflag allow the server to skip the logging, but it's not supported, not documented and... really dangerous !|||Originally posted by fadace
Fortunately (in a consistancy point of view), any update is logged, in user tables, system tables, worktables or tables in tempdb.

A specific traceflag allow the server to skip the logging, but it's not supported, not documented and... really dangerous !

Huh? Well I'm glad you didn't become very specific about what you're talking about.

Veritant:

You say Search to return result set, then you say update to work table

SELECTS are not logged

Creating a result set on the fly, like

SELECT * INTO #temp FROM...is not logged

TRUNCATE TABLE or DROP TABLE is not logged..

If you have some examples of what you're doing and need help, post it and we'll look at it...

Avoiding Temporary Tables

Hello All,
I have a question about how I might avoid using a temporary table in a sql
query. In a query I am writing I am trying to extract data from a table o
n
an existing database via a set of identifier codes (These queries are writte
n
from the Matlab environment and the list of identifiers are easily accesible
in matlab).
The query I am currently using looks something like this...
select t.var1, t.code, t.var2
from pinf t
join
( SELECT distinct s.pcode as code
FROM prc s
WHERE s.code in (74156, 54471) ) -- identifiers 74156, 54471
as prctable on prctable.code = t.code
and '12/06/2005' >= t.dt1 and t.d2 <= '12/06/2005'
with the identifier being 74156, 54471. The problem is there is not a
convenient table with all of the identifiers in it. So the solution above
where I draw from an existing table is not sufficient.
I believe that I can create a temporary table with these identifiers in it,
but the performance of the query slows markedly.
Is there some other way to extract the data with these identifier from the
table pinf without creating a temporary table?
Any help would be greatly appreciated.
Cheers,
Lanny>> ... with the identifier being 74156, 54471.
Is this a set of two identifier values or a single comma separated
identifier value with two numbers in them?
I am not familiar with the matlab environment, but how exactly do you
extract the identifiers to the #temp table? The general alternatives ( which
in many cases may not be sufficient ) are using a view or even a base table
which can have all the required identifiers.
Anith|||It is a set of 2 distinct identifier, and conceptually I would want to look
for many more than 2 (perhaps on the order of thousands) of these distinct
identifiers.
I extract the identifiers via another query that I know functions properly
and will give me the desired identifier. I looked into using a base table
or a view, and did not believe they provided the functionality I needed.
Essentially, I have some list of numbers and I want to extract that list, bu
t
all of the elements of this list do not exist on any distinct table in the
database.
Thanks so much for the help.
"Anith Sen" wrote:

> Is this a set of two identifier values or a single comma separated
> identifier value with two numbers in them?
>
> I am not familiar with the matlab environment, but how exactly do you
> extract the identifiers to the #temp table? The general alternatives ( whi
ch
> in many cases may not be sufficient ) are using a view or even a base tabl
e
> which can have all the required identifiers.
> --
> Anith
>
>|||On Wed, 7 Dec 2005 10:18:03 -0800, Lanny wrote:

>It is a set of 2 distinct identifier, and conceptually I would want to look
>for many more than 2 (perhaps on the order of thousands) of these distinct
>identifiers.
>I extract the identifiers via another query that I know functions properly
>and will give me the desired identifier. I looked into using a base table
>or a view, and did not believe they provided the functionality I needed.
>Essentially, I have some list of numbers and I want to extract that list, b
ut
>all of the elements of this list do not exist on any distinct table in the
>database.
>Thanks so much for the help.
Hi Lanny,
I'm not sure if I understand your requirements completely, but based on
what I do understand, you might find what you need on Erlands site:
http://www.sommarskog.se/arrays-in-sql.html
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)

Avoiding temp tables

Dear all,
I'd like to rewrite this update statement without using a temp table.
For each row with duplicate my_id's, the reference_no field should be
set to the number of duplicates for that id.
When I try rewriting this as a single statement I have problems getting
'at' the calculated duplicates field.
Cheers!
select my_id, count(*) as duplicates
into #tmp
from my_table
group by my_id
having count(*) > 1
update my_table
set my_table.reference_no = #tmp.duplicates
from #tmp, my_table
where #tmp.my_id = my_table.my_idOn 7 Mar 2005 23:53:24 -0800, davidol@.hushmail.com wrote:

>I'd like to rewrite this update statement without using a temp table.
>For each row with duplicate my_id's, the reference_no field should be
>set to the number of duplicates for that id.
Hi Davidol,
This version uses only ANSI-standard constructions. You need to use the
column(s) that make up the primary key of the table; I've assumed a
compound primary key on column PK01 and PK02 for my example:
UPDATE my_table
SET reference_no = (SELECT COUNT(*)
FROM my_table AS m2
WHERE m2.my_id = my_table.my_id)
WHERE EXISTS (SELECT *
FROM my_table AS m2
WHERE m2.my_id = my_table.my_id
AND ( m2.PK01 <> my_table.PK01
OR m2.PK02 <> my_table.PK02))
If you don't have a primary key, you should change your design. In case
you can't do that right now, try the following query (still ANSI
compliant, but probably slower than the first query):
UPDATE my_table
SET reference_no = (SELECT COUNT(*)
FROM my_table AS m2
WHERE m2.my_id = my_table.my_id)
WHERE (SELECT COUNT(*)
FROM my_table AS m2
WHERE m2.my_id = my_table.my_id) > 1
Finally, if you don't care about portability, you could use the
proprietary UPDATE FROM syntax, as below. Performance might be better
than the ANSI-compliant version (but test it out to be sure). Don't
forget to document the use of a non-ANSI compliant construction (and
include a commented ANSI-compliant version in the code, or include it in
external documentation, so that you don't have to redo the thinking when
you do have to port your code).
UPDATE m
SET m.reference_no = a.cnt
FROM my_table AS m
INNER JOIN (SELECT my_id, COUNT(*) AS cnt
FROM my_table
GROUP BY my_id
HAVING COUNT(*) > 1) AS a
ON a.my_id = m.my_id
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Try this also
update my_table set reference=T.Count from (
select my_id,count(*) as 'Count' from my_table group by my_id having
count(*)>1)
T , my_table A where A.my_id=T.my_id
Madhivanan

Friday, February 24, 2012

avoiding duplicate lines

I have a oracle database that includes the following tables:

game(gameID, season, week, stadium) -- gameID is PK

competesIn(gameID, team, score) -- gameID, teamName is PK

As you can probably infer, it is a database to track the results of football games.

competesIn has two records for every game, one for each team.

I would like to create a view that combines these tables into the following structure

gameView(season, week, location, team1, score1, team2, score2)

I have tried using the following SQL inside the view:

select g.season, g.week, g.stadium,
c1.team as team1, c1.score as Score1,
c2.team as team2, c2.score as Score2
from game g, competesIn c1, competesIn c2
where c1.gameID = g.gameID
and c2.gameID = g.gameID
and c1.team != c2.team

This does create the view I was looking for but it creates two rows for each game and I need it to return only one row per game.

Any thoughts?change != in last line to <

Avoiding a horrific database structure.

Hi,
I hope someone can shed some light on a project that I've
been given, and how I can go about managing the structure
of the tables because it's driving me mad as to find a
good solution.
In short I've in the middle of creating a messaging system
with apporximately 20,000 users. One user will post a
message (text only) onto the server, and they can decide
who it goes to. This 'distribution' can be one, or many
recipients. It isn't email I hasten to add, just a simple
messaging system.
The trouble is, that the sender needs to know if the
recipient(s) have read the message.
It sounds simple, but there are three possibilities, 2 of
which could prove a huge overhaul on the server.
The first is to create a table called 'Messages', each
message has a unique identifier, and is replicated each
time to each and every recipient. So if we had 20,000
users, half of which had received the same message, the
table could be like...
messageID recipientID read
1 1 false
1 2 false
1 3 true
1 4 false
[...]
1 20000 true
As you can appreciate, that would be about 20000 records
per message! There could be over hundred messages a day,
and the user list is growing day by day!
The other option would be an individual table per user,
This would reduce the size of one table dramatically, but
we'd end up with 20000+ tables, for example
Table 1
messageID read
1 true
4 false
19 false
Table 2
2 false
91 false
109 true
etc...
I'm sure there has to be an easier way of doing this, any
ideas would be GREATLY appreciated.
TIAJason
I would think your message table would be the best way.
You are estimating up to 2,000,000 rows a day, that is not
a lot for SQL Server to handle, with a clustered index on
the receipientID (possibily a composite with messageID as
well), it should run just fine. The real question is how
long do you keep the messages? While 2,000,000 rows is not
a lot for SQL Server, after a few months of that kind of
volume, it would become quite large.
What are the plans for keeping or deleting or archiving
old messages?
Regards
John|||Jason
/*
The other option would be an individual table per user,
This would reduce the size of one table dramatically, but
we'd end up with 20000+ tables, for example
*/
Don't do it. It is very bad design issue .
If I undestand you correctly one user may has many messages and one message
may has many users. Am I right?
I'd go to create three tables Users,Messages ,Message_Users
create table Users
(
userid int not null primary key
username varchar(50) not null
.....
......
)
-- to establish many-many relationship between user and message
create table Message_Users
(
id int not null primary key,
userid int not null REFERENCES users (userid)
messageid int not null REFERENCES messages (messageid )
read bit 'default 0'
)
create table Messages
(
messageid int not null primary key,
messagename varchar(8000)
.....
.....
)
"Jason Hirst" <jasonmhirst@.hotmail.com> wrote in message
news:0a9901c35b28$240ae8a0$a101280a@.phx.gbl...
> Hi,
> I hope someone can shed some light on a project that I've
> been given, and how I can go about managing the structure
> of the tables because it's driving me mad as to find a
> good solution.
> In short I've in the middle of creating a messaging system
> with apporximately 20,000 users. One user will post a
> message (text only) onto the server, and they can decide
> who it goes to. This 'distribution' can be one, or many
> recipients. It isn't email I hasten to add, just a simple
> messaging system.
> The trouble is, that the sender needs to know if the
> recipient(s) have read the message.
> It sounds simple, but there are three possibilities, 2 of
> which could prove a huge overhaul on the server.
> The first is to create a table called 'Messages', each
> message has a unique identifier, and is replicated each
> time to each and every recipient. So if we had 20,000
> users, half of which had received the same message, the
> table could be like...
> messageID recipientID read
> 1 1 false
> 1 2 false
> 1 3 true
> 1 4 false
> [...]
> 1 20000 true
> As you can appreciate, that would be about 20000 records
> per message! There could be over hundred messages a day,
> and the user list is growing day by day!
> The other option would be an individual table per user,
> This would reduce the size of one table dramatically, but
> we'd end up with 20000+ tables, for example
> Table 1
> messageID read
> 1 true
> 4 false
> 19 false
> Table 2
> 2 false
> 91 false
> 109 true
> etc...
> I'm sure there has to be an easier way of doing this, any
> ideas would be GREATLY appreciated.
> TIA|||> The trouble is, that the sender needs to know if the
> recipient(s) have read the message.
For all 20000 recipients' I suggest you have a good look at your business
requirements to see if this is really necessary.
Anyway, you should go with having one table for all the messages, the amount
of data you store per row is very small, only 9 bytes, and 2 million rows
per day is not something SQL Server 2000 can't handle. You also have to look
at how long you want to keep the information and when you can start cleaning
out the table or moving it to an archive table. I assume nobody is really
interested in messages of more than a few months old.
Imagine you would use 20000 different tables, one for each user to keep
track of each messages s/he has read, how do you get the information for the
sender about who has read the message?
SELECT read FROM table1 WHERE message_id = 1
UNION ALL
SELECT read FROM table2 WHERE message_id = 1
UNION ALL
SELECT read FROM table3 WHERE message_id = 1
etc.. ?
--
Jacco Schalkwijk MCDBA, MCSD, MCSE
Database Administrator
Eurostop Ltd.
"Jason Hirst" <jasonmhirst@.hotmail.com> wrote in message
news:0a9901c35b28$240ae8a0$a101280a@.phx.gbl...
> Hi,
> I hope someone can shed some light on a project that I've
> been given, and how I can go about managing the structure
> of the tables because it's driving me mad as to find a
> good solution.
> In short I've in the middle of creating a messaging system
> with apporximately 20,000 users. One user will post a
> message (text only) onto the server, and they can decide
> who it goes to. This 'distribution' can be one, or many
> recipients. It isn't email I hasten to add, just a simple
> messaging system.
> The trouble is, that the sender needs to know if the
> recipient(s) have read the message.
> It sounds simple, but there are three possibilities, 2 of
> which could prove a huge overhaul on the server.
> The first is to create a table called 'Messages', each
> message has a unique identifier, and is replicated each
> time to each and every recipient. So if we had 20,000
> users, half of which had received the same message, the
> table could be like...
> messageID recipientID read
> 1 1 false
> 1 2 false
> 1 3 true
> 1 4 false
> [...]
> 1 20000 true
> As you can appreciate, that would be about 20000 records
> per message! There could be over hundred messages a day,
> and the user list is growing day by day!
> The other option would be an individual table per user,
> This would reduce the size of one table dramatically, but
> we'd end up with 20000+ tables, for example
> Table 1
> messageID read
> 1 true
> 4 false
> 19 false
> Table 2
> 2 false
> 91 false
> 109 true
> etc...
> I'm sure there has to be an easier way of doing this, any
> ideas would be GREATLY appreciated.
> TIA|||Thanks for the reply john.
I'd estimate that messages would be kept for 90 days. We
would try and enforce some housekeeping, but of course you
know users... they don't always listen, and it's a growing
concern (no pun intended) that the size of the table will
reach a stupid size. I agree 2,000,000 rows a day may not
be much for SQL to handle, but with 20000 users, each
posting to oneanother, or 'distribution lists', the
constant access on this table alone would be incredible.
I didn't know if there was some kind of alogrithm to work
it out. I've had scraps of paper on my floor all night,
trying to work it out.
I did think about having one huge hexadecimal string to
indicate those who had received the message, for example
34DC9AC145ED, if it were 4byte per record, would break
down to user id's 13532, 39617 and 17901. And I could say
that the aforementioned code, because it contained all
three user ID's, would mean that all three had NOT read
the message, but 34DC45ED would mean that one user (39617)
had read the message because they weren't listed. Thus,
the size of this field would reduce as more and more
people read the message that it was intended for!! (I
hope I explained that right!!)
Any ideas on how Outlook handles things like receipts in
it's tracking system?
TIA
>--Original Message--
>Jason
>I would think your message table would be the best way.
>You are estimating up to 2,000,000 rows a day, that is
not
>a lot for SQL Server to handle, with a clustered index on
>the receipientID (possibily a composite with messageID as
>well), it should run just fine. The real question is how
>long do you keep the messages? While 2,000,000 rows is
not
>a lot for SQL Server, after a few months of that kind of
>volume, it would become quite large.
>What are the plans for keeping or deleting or archiving
>old messages?
>Regards
>John
>.
>|||It's true to say that all 20000 recipients could received
at LEAST one message a day from someone else.
The 'message' table would be structured similar to...
messageID title description sender
(autoident) nvarchar nvarchar int
Where the sender field is joined to the userID in the
users table.
A 'similar' table to track messages would be...
messageID recipient
(int) (int)
I agree that there should be some good housekeeping to
delete messages, but this has to be on BOTH parties,
because the sender would need to clean out their 'sent'
items, and the recipients clean out their 'received'
items, and vice-versa.
>--Original Message--
>> The trouble is, that the sender needs to know if the
>> recipient(s) have read the message.
>For all 20000 recipients' I suggest you have a good look
at your business
>requirements to see if this is really necessary.
>Anyway, you should go with having one table for all the
messages, the amount
>of data you store per row is very small, only 9 bytes,
and 2 million rows
>per day is not something SQL Server 2000 can't handle.
You also have to look
>at how long you want to keep the information and when you
can start cleaning
>out the table or moving it to an archive table. I assume
nobody is really
>interested in messages of more than a few months old.
>Imagine you would use 20000 different tables, one for
each user to keep
>track of each messages s/he has read, how do you get the
information for the
>sender about who has read the message?
>SELECT read FROM table1 WHERE message_id = 1
>UNION ALL
>SELECT read FROM table2 WHERE message_id = 1
>UNION ALL
>SELECT read FROM table3 WHERE message_id = 1
>etc.. ?
>--
>Jacco Schalkwijk MCDBA, MCSD, MCSE
>Database Administrator
>Eurostop Ltd.
>
>"Jason Hirst" <jasonmhirst@.hotmail.com> wrote in message
>news:0a9901c35b28$240ae8a0$a101280a@.phx.gbl...
>> Hi,
>> I hope someone can shed some light on a project that
I've
>> been given, and how I can go about managing the
structure
>> of the tables because it's driving me mad as to find a
>> good solution.
>> In short I've in the middle of creating a messaging
system
>> with apporximately 20,000 users. One user will post a
>> message (text only) onto the server, and they can decide
>> who it goes to. This 'distribution' can be one, or many
>> recipients. It isn't email I hasten to add, just a
simple
>> messaging system.
>> The trouble is, that the sender needs to know if the
>> recipient(s) have read the message.
>> It sounds simple, but there are three possibilities, 2
of
>> which could prove a huge overhaul on the server.
>> The first is to create a table called 'Messages', each
>> message has a unique identifier, and is replicated each
>> time to each and every recipient. So if we had 20,000
>> users, half of which had received the same message, the
>> table could be like...
>> messageID recipientID read
>> 1 1 false
>> 1 2 false
>> 1 3 true
>> 1 4 false
>> [...]
>> 1 20000 true
>> As you can appreciate, that would be about 20000 records
>> per message! There could be over hundred messages a
day,
>> and the user list is growing day by day!
>> The other option would be an individual table per user,
>> This would reduce the size of one table dramatically,
but
>> we'd end up with 20000+ tables, for example
>> Table 1
>> messageID read
>> 1 true
>> 4 false
>> 19 false
>> Table 2
>> 2 false
>> 91 false
>> 109 true
>> etc...
>> I'm sure there has to be an easier way of doing this,
any
>> ideas would be GREATLY appreciated.
>> TIA
>
>.
>|||> but with 20000 users, each
> posting to oneanother, or 'distribution lists', the
> constant access on this table alone would be incredible.
Yes, but they would all be accessing different rows, the only two people who
would access the same row are the sender and the recipient. If you, as John
suggested, have indexes on this table, access would be very quick and it
would hardly occur that users have to wait for each other to access a row.
Most of the access would be SELECTs, which can be concurrent anyway, and the
only changes that happen the table are the inserts of new messages and the
update of an existing messages whena recipient reads it, but these never
conflict.
--
Jacco Schalkwijk MCDBA, MCSD, MCSE
Database Administrator
Eurostop Ltd.
"Jason Hirst" <jasonmhirst@.hotmail.com> wrote in message
news:09c801c35b2e$c99452b0$a401280a@.phx.gbl...
> Thanks for the reply john.
> I'd estimate that messages would be kept for 90 days. We
> would try and enforce some housekeeping, but of course you
> know users... they don't always listen, and it's a growing
> concern (no pun intended) that the size of the table will
> reach a stupid size. I agree 2,000,000 rows a day may not
> be much for SQL to handle, but with 20000 users, each
> posting to oneanother, or 'distribution lists', the
> constant access on this table alone would be incredible.
> I didn't know if there was some kind of alogrithm to work
> it out. I've had scraps of paper on my floor all night,
> trying to work it out.
> I did think about having one huge hexadecimal string to
> indicate those who had received the message, for example
> 34DC9AC145ED, if it were 4byte per record, would break
> down to user id's 13532, 39617 and 17901. And I could say
> that the aforementioned code, because it contained all
> three user ID's, would mean that all three had NOT read
> the message, but 34DC45ED would mean that one user (39617)
> had read the message because they weren't listed. Thus,
> the size of this field would reduce as more and more
> people read the message that it was intended for!! (I
> hope I explained that right!!)
> Any ideas on how Outlook handles things like receipts in
> it's tracking system?
> TIA
> >--Original Message--
> >Jason
> >
> >I would think your message table would be the best way.
> >You are estimating up to 2,000,000 rows a day, that is
> not
> >a lot for SQL Server to handle, with a clustered index on
> >the receipientID (possibily a composite with messageID as
> >well), it should run just fine. The real question is how
> >long do you keep the messages? While 2,000,000 rows is
> not
> >a lot for SQL Server, after a few months of that kind of
> >volume, it would become quite large.
> >
> >What are the plans for keeping or deleting or archiving
> >old messages?
> >
> >Regards
> >
> >John
> >.
> >|||200,000,000!!!
Looks like I'm going to have to have a serious rethink on
this (as is my client!!)
Thanks for the help.
>--Original Message--
>Jason
>90 days, thats under 200,000,000 million if the system is
>used 7 days a week, under 150,000,000 if only 5 days a
>week. It will have very small rows, just a lot of them.
>With the correct index and regular rebuilds, weekly at
>least, it should run all right as long as you get a
>powerful enough system.
>As well as a good design I think you need to do some
>serious sizing and modeling, to make sure you have a
>suitable server.
>You will need a good maint window to perform the
deletions
>and the index rebuilds.
>Good luck
>John
>.
>|||Jason,
I'm sorry to sound condescending, but you probably need a serious read up on
the capabilities and architecture of SQL Server (Inside SQL Server by Kalen
Delaney is a very good book) instead of a serious rethink about your
application. As John and me have been arguing for quite a few posts, 200
million small rows is not something to worry very much about on SQL Server,
we are not talking about Access here :) I am willing to bet you that the
alternative designs you have been thinking about will perform lots worse
than the design we have been supporting, even though they might have less
rows. Don't be "scared" of the large number of rows, SQL Server is designed
to handle them.
The largest table in the systems I am responsible for has about 25 million
rows and 60 columns (compared to 200 million rows and 3 columns in your
scenario), and although performance could probably still be improved
somewhat, it is acceptable. It is running on a 4 processor box with 4 GB of
memory, a reasonably heavy box, but not out of the ordinary, and it is
definitly not being pushed to its limits.
The key to good performance in this case is having proper indexes and time
to do index rebuilds.
--
Jacco Schalkwijk MCDBA, MCSD, MCSE
Database Administrator
Eurostop Ltd.
"Jason Hirst" <jasonmhirst@.hotmail.com> wrote in message
news:0b7b01c35b36$15289a90$a101280a@.phx.gbl...
> 200,000,000!!!
> Looks like I'm going to have to have a serious rethink on
> this (as is my client!!)
> Thanks for the help.
>
> >--Original Message--
> >Jason
> >
> >90 days, thats under 200,000,000 million if the system is
> >used 7 days a week, under 150,000,000 if only 5 days a
> >week. It will have very small rows, just a lot of them.
> >With the correct index and regular rebuilds, weekly at
> >least, it should run all right as long as you get a
> >powerful enough system.
> >
> >As well as a good design I think you need to do some
> >serious sizing and modeling, to make sure you have a
> >suitable server.
> >
> >You will need a good maint window to perform the
> deletions
> >and the index rebuilds.
> >
> >Good luck
> >
> >John
> >.
> >|||Jason,
I would assume that the users do not randomly select 1000 recipient among
the 20000. They may send a message to a group that has a certain logical
meaning, like all in dept. x, all with last name starting with D, etc.
Beyond that, they may also send a message to everyone, to a handful (1 or
several) of interactively selected.
If this assumption holds true, I suggest the following:
A recipients table, holding all the recipients;
A recipient table, holding recipient groups and their members (one record
for each recipient);
A messageSent table, holding MessageId, Recipient (one record for each
recipient group and for each recipient if they are specified outside group);
A messageRead table, holding messageId, Recipient (one record for each
individual recipient who has read the message).
This way you significantly reduce the original sent entries, and keep read
record for those who did read. You can consider put the first two tables
into one (by now you should realize that the recipients and group is very
similar to sql server's users and groups structure).
Quentin
"Jason Hirst" <jasonmhirst@.hotmail.com> wrote in message
news:0a9901c35b28$240ae8a0$a101280a@.phx.gbl...
> Hi,
> I hope someone can shed some light on a project that I've
> been given, and how I can go about managing the structure
> of the tables because it's driving me mad as to find a
> good solution.
> In short I've in the middle of creating a messaging system
> with apporximately 20,000 users. One user will post a
> message (text only) onto the server, and they can decide
> who it goes to. This 'distribution' can be one, or many
> recipients. It isn't email I hasten to add, just a simple
> messaging system.
> The trouble is, that the sender needs to know if the
> recipient(s) have read the message.
> It sounds simple, but there are three possibilities, 2 of
> which could prove a huge overhaul on the server.
> The first is to create a table called 'Messages', each
> message has a unique identifier, and is replicated each
> time to each and every recipient. So if we had 20,000
> users, half of which had received the same message, the
> table could be like...
> messageID recipientID read
> 1 1 false
> 1 2 false
> 1 3 true
> 1 4 false
> [...]
> 1 20000 true
> As you can appreciate, that would be about 20000 records
> per message! There could be over hundred messages a day,
> and the user list is growing day by day!
> The other option would be an individual table per user,
> This would reduce the size of one table dramatically, but
> we'd end up with 20000+ tables, for example
> Table 1
> messageID read
> 1 true
> 4 false
> 19 false
> Table 2
> 2 false
> 91 false
> 109 true
> etc...
> I'm sure there has to be an easier way of doing this, any
> ideas would be GREATLY appreciated.
> TIA|||Well I've had a jiggle about with the design, and have
come with a compromise regarding it all.
I 'think' that the best way to approach this is to have
half SQL and half XML. Let me explain why and how...
The system I know 100% will get abuse by several members,
namely 'messaging' all 20,000 contacts on a very regular
basis. By regular, I mean 2 to 3 times a day. In one day
alone, there could be over 200,000 rows added to the table.
Therefore, I thought about having messages in a 'holding
pen', which when read, are saved on the server as an XML
file, with the option to download later. This would allow
the work of the SQL server to be at normal levels, and the
size of the table not reaching horrific proportions.
When the user logs in to the system, it checks to see if
there are any new messages on the server (easy enough), if
there are, then it does NOT transfer them to the XML file
until they have read the message. This would also save a
field for "read message" because if the message was on the
server, then obviously it hasn't been read. I appreciate
I'm not saving a great deal of space, 200,000 fields of
boolean datatype wouldn't be a great threat to the server,
but every penny counts!
Thanks for the help, it has been apprecitated.
>--Original Message--
>Jason,
>I'm sorry to sound condescending, but you probably need a
serious read up on
>the capabilities and architecture of SQL Server (Inside
SQL Server by Kalen
>Delaney is a very good book) instead of a serious rethink
about your
>application. As John and me have been arguing for quite a
few posts, 200
>million small rows is not something to worry very much
about on SQL Server,
>we are not talking about Access here :) I am willing to
bet you that the
>alternative designs you have been thinking about will
perform lots worse
>than the design we have been supporting, even though they
might have less
>rows. Don't be "scared" of the large number of rows, SQL
Server is designed
>to handle them.
>The largest table in the systems I am responsible for has
about 25 million
>rows and 60 columns (compared to 200 million rows and 3
columns in your
>scenario), and although performance could probably still
be improved
>somewhat, it is acceptable. It is running on a 4
processor box with 4 GB of
>memory, a reasonably heavy box, but not out of the
ordinary, and it is
>definitly not being pushed to its limits.
>The key to good performance in this case is having proper
indexes and time
>to do index rebuilds.
>--
>Jacco Schalkwijk MCDBA, MCSD, MCSE
>Database Administrator
>Eurostop Ltd.
>
>"Jason Hirst" <jasonmhirst@.hotmail.com> wrote in message
>news:0b7b01c35b36$15289a90$a101280a@.phx.gbl...
>> 200,000,000!!!
>> Looks like I'm going to have to have a serious rethink
on
>> this (as is my client!!)
>> Thanks for the help.
>>
>> >--Original Message--
>> >Jason
>> >
>> >90 days, thats under 200,000,000 million if the system
is
>> >used 7 days a week, under 150,000,000 if only 5 days a
>> >week. It will have very small rows, just a lot of them.
>> >With the correct index and regular rebuilds, weekly at
>> >least, it should run all right as long as you get a
>> >powerful enough system.
>> >
>> >As well as a good design I think you need to do some
>> >serious sizing and modeling, to make sure you have a
>> >suitable server.
>> >
>> >You will need a good maint window to perform the
>> deletions
>> >and the index rebuilds.
>> >
>> >Good luck
>> >
>> >John
>> >.
>> >
>
>.
>|||So if I understand you correctly you are going to save the cost of a 1 byte
column in a database table by storing 200,000 XML files?
I am quite disappointed that you come here on this newsgroup for expert
advice (which is totally free as well), you get good advice from some
people, and basically you just ignore it and go on believing what you
believed before. You 'believe' things, because you have never tested if the
solution that has been proposed here would work properly. It would only take
about half a day to set up a test with a million row table, so you could
have solid results to base your design decisions on.
Let me repeat it one more time:
200,000 rows per day is NOT A PROBLEM for SQL Server.
--
Jacco Schalkwijk MCDBA, MCSD, MCSE
Database Administrator
Eurostop Ltd.
"Jason Hirst" <jasonmhirst@.hotmail.com> wrote in message
news:081801c35bed$63dc8900$a401280a@.phx.gbl...
> Well I've had a jiggle about with the design, and have
> come with a compromise regarding it all.
> I 'think' that the best way to approach this is to have
> half SQL and half XML. Let me explain why and how...
> The system I know 100% will get abuse by several members,
> namely 'messaging' all 20,000 contacts on a very regular
> basis. By regular, I mean 2 to 3 times a day. In one day
> alone, there could be over 200,000 rows added to the table.
> Therefore, I thought about having messages in a 'holding
> pen', which when read, are saved on the server as an XML
> file, with the option to download later. This would allow
> the work of the SQL server to be at normal levels, and the
> size of the table not reaching horrific proportions.
> When the user logs in to the system, it checks to see if
> there are any new messages on the server (easy enough), if
> there are, then it does NOT transfer them to the XML file
> until they have read the message. This would also save a
> field for "read message" because if the message was on the
> server, then obviously it hasn't been read. I appreciate
> I'm not saving a great deal of space, 200,000 fields of
> boolean datatype wouldn't be a great threat to the server,
> but every penny counts!
> Thanks for the help, it has been apprecitated.
>
> >--Original Message--
> >Jason,
> >
> >I'm sorry to sound condescending, but you probably need a
> serious read up on
> >the capabilities and architecture of SQL Server (Inside
> SQL Server by Kalen
> >Delaney is a very good book) instead of a serious rethink
> about your
> >application. As John and me have been arguing for quite a
> few posts, 200
> >million small rows is not something to worry very much
> about on SQL Server,
> >we are not talking about Access here :) I am willing to
> bet you that the
> >alternative designs you have been thinking about will
> perform lots worse
> >than the design we have been supporting, even though they
> might have less
> >rows. Don't be "scared" of the large number of rows, SQL
> Server is designed
> >to handle them.
> >
> >The largest table in the systems I am responsible for has
> about 25 million
> >rows and 60 columns (compared to 200 million rows and 3
> columns in your
> >scenario), and although performance could probably still
> be improved
> >somewhat, it is acceptable. It is running on a 4
> processor box with 4 GB of
> >memory, a reasonably heavy box, but not out of the
> ordinary, and it is
> >definitly not being pushed to its limits.
> >
> >The key to good performance in this case is having proper
> indexes and time
> >to do index rebuilds.
> >
> >--
> >Jacco Schalkwijk MCDBA, MCSD, MCSE
> >Database Administrator
> >Eurostop Ltd.
> >
> >
> >"Jason Hirst" <jasonmhirst@.hotmail.com> wrote in message
> >news:0b7b01c35b36$15289a90$a101280a@.phx.gbl...
> >> 200,000,000!!!
> >>
> >> Looks like I'm going to have to have a serious rethink
> on
> >> this (as is my client!!)
> >>
> >> Thanks for the help.
> >>
> >>
> >> >--Original Message--
> >> >Jason
> >> >
> >> >90 days, thats under 200,000,000 million if the system
> is
> >> >used 7 days a week, under 150,000,000 if only 5 days a
> >> >week. It will have very small rows, just a lot of them.
> >> >With the correct index and regular rebuilds, weekly at
> >> >least, it should run all right as long as you get a
> >> >powerful enough system.
> >> >
> >> >As well as a good design I think you need to do some
> >> >serious sizing and modeling, to make sure you have a
> >> >suitable server.
> >> >
> >> >You will need a good maint window to perform the
> >> deletions
> >> >and the index rebuilds.
> >> >
> >> >Good luck
> >> >
> >> >John
> >> >.
> >> >
> >
> >
> >.
> >|||Hi,
Why not you just take note of how many has not read your message?
the record will be lesser and may be zero.
remove those id record that have been read..
won't it be better?
you table size will always be very small.
Best Regards,
Wind
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Avoid Looping / Cursors. Help with Statement.

Hi Folks,
I have two tables, one of which I want to update from another.
Essentiall I have a table of orders and a product table. I want to
subtract the qty sold in the orders table from the QtyInStock column in
the Products table, for every line in an order.
But I dont really want to loop through each line in the order, either
in application or with a cursor as something is telling me there must
be a neater solution!
Example Orders Table.
OrderID ProductID Qty
1, 104, 2
1, 199, 1
2, 100, 3
3, 858, 1
ProductID, QtyInStock
104, 3
199, 1
etc ...
As you can see I want to be able to run a query against OrderID 1,
and it to reduce the QtyInStock column by the correct amount for
products 104 and 199 as an example.
Can this be done with one statement, or will I have to loop ?
Thanks in Advance.
Craig.Hi
CREATE TABLE #Test1
(
OrderID int,
ProductID int,
Qty int
)
INSERT INTO #Test1 VALUES (1,104,2)
INSERT INTO #Test1 VALUES (1,199,1)
INSERT INTO #Test1 VALUES (2,100,3)
INSERT INTO #Test1 VALUES (3,828,1)
CREATE TABLE #Test2
(
ProductID int,
QtyInStock int
)
INSERT INTO #Test2 VALUES (104,3)
INSERT INTO #Test2 VALUES (199,1)
UPDATE #Test1 SET Qty=(SELECT QtyInStock-Qty
FROM #Test2 T WHERE T.ProductID=#Test1.ProductID)
WHERE EXISTS (SELECT * FROM #Test2
T WHERE T.ProductID=#Test1.ProductID)
DROP TABLE #Test1,#Test2
<craig.parsons@.crawfos.com> wrote in message
news:1136476091.334221.282250@.g44g2000cwa.googlegroups.com...
> Hi Folks,
> I have two tables, one of which I want to update from another.
> Essentiall I have a table of orders and a product table. I want to
> subtract the qty sold in the orders table from the QtyInStock column in
> the Products table, for every line in an order.
> But I dont really want to loop through each line in the order, either
> in application or with a cursor as something is telling me there must
> be a neater solution!
> Example Orders Table.
> OrderID ProductID Qty
> 1, 104, 2
> 1, 199, 1
> 2, 100, 3
> 3, 858, 1
> ProductID, QtyInStock
> 104, 3
> 199, 1
> etc ...
> As you can see I want to be able to run a query against OrderID 1,
> and it to reduce the QtyInStock column by the correct amount for
> products 104 and 199 as an example.
> Can this be done with one statement, or will I have to loop ?
>
> Thanks in Advance.
>
> Craig.
>|||Uri,
I think Craig wants to update the quantity in stock
from the Product table, not the quantity in the Orders
table, which your query updates. He could use Jens's
solution, or one like this:
UPDATE #Products SET
QtyInStock = QtyInStock - (
SELECT SUM(O.Qty)
FROM #Orders AS O
WHERE O.ProductID = #Products.ProductID
)
WHERE EXISTS (
SELECT * FROM #Orders
WHERE #Orders.ProductID = #Products.ProductID
)
Steve Kass
Drew University
Uri Dimant wrote:

>Hi
>CREATE TABLE #Test1
>(
> OrderID int,
> ProductID int,
> Qty int
> )
>INSERT INTO #Test1 VALUES (1,104,2)
>INSERT INTO #Test1 VALUES (1,199,1)
>INSERT INTO #Test1 VALUES (2,100,3)
>INSERT INTO #Test1 VALUES (3,828,1)
>
>CREATE TABLE #Test2
>(
> ProductID int,
> QtyInStock int
> )
>INSERT INTO #Test2 VALUES (104,3)
>INSERT INTO #Test2 VALUES (199,1)
>
>UPDATE #Test1 SET Qty=(SELECT QtyInStock-Qty
>FROM #Test2 T WHERE T.ProductID=#Test1.ProductID)
>WHERE EXISTS (SELECT * FROM #Test2
>T WHERE T.ProductID=#Test1.ProductID)
>
>
>DROP TABLE #Test1,#Test2
>
>
><craig.parsons@.crawfos.com> wrote in message
>news:1136476091.334221.282250@.g44g2000cwa.googlegroups.com...
>
>
>|||craig.parsons@.crawfos.com wrote:
> Hi Folks,
> I have two tables, one of which I want to update from another.
> Essentiall I have a table of orders and a product table. I want to
> subtract the qty sold in the orders table from the QtyInStock column
> in the Products table, for every line in an order.
> But I dont really want to loop through each line in the order,
> either in application or with a cursor as something is telling me
> there must be a neater solution!
> Example Orders Table.
> OrderID ProductID Qty
> 1, 104, 2
> 1, 199, 1
> 2, 100, 3
> 3, 858, 1
> ProductID, QtyInStock
> 104, 3
> 199, 1
> etc ...
> As you can see I want to be able to run a query against OrderID 1,
> and it to reduce the QtyInStock column by the correct amount for
> products 104 and 199 as an example.
> Can this be done with one statement, or will I have to loop ?
>
Here is the ANSI version (I used the Sum function to guarantee only a single
result would be returned):
UPDATE Products
SET QtyInStock = QtyInStock -
(SELECT Sum(Qty) FROM Orders o
WHERE o.OrderID = 1 AND o.ProductID = Products.ProductID)
The T-SQL version:
UPDATE p
SET QtyInStock = QtyInStock - o.Qty
FROM Products p inner join (
SELECT ProductID,Sum(Qty) AS Qty FROM Orders
WHERE OrderID = 1 GROUP BY ProductID) o
ON o.ProductID = p.ProductID
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.

Avoid Inserting Duplicate Entries

Hi all,

I am somewhat new to sql server. So help me in whatever way you can.

I have two tables one of which doesnt have a primary key(table A) and other has a composite key formed of two columns(table B). I need to insert entries in table B from table A(table A has a unique constratint UID which is passed to table B). Table B also has a unique ID which is different from UID of table A.

Sometimes table A tries to insert a duplicate entry which is denied by table B since it has a primary key.

The insert query is as below.

table A - CORE
table B - CRF

INSERT INTO CRF (CORE_UID,ACCT_NUM_MIN, ACCT_NUM_MAX,BIN, BUS_ID,BUS_NM,ISO_CTRY_CD, REGN_CD, PROD_TYPE_CD, CARD_TYPE)
(SELECT UID,LEFT(ACCT_NUM_MIN,16),LEFT( ACCT_NUM_MAX,16),BIN, BUS_ID,BUS_NM,ISO_CTRY_CD, REGN_CD, PROD_TYPE_CD, CARD_TYPE FROM CORE WHERE UID NOT IN (SELECT CORE_UID FROM CRF))

I am not worried about the performance. I have tried if not exists and except but since i am new i am not able to figure out the problem.

Thank you in advance for all who try to help me out.

Quote:

Originally Posted by desirocks

Hi all,

I am somewhat new to sql server. So help me in whatever way you can.

I have two tables one of which doesnt have a primary key(table A) and other has a composite key formed of two columns(table B). I need to insert entries in table B from table A(table A has a unique constratint UID which is passed to table B). Table B also has a unique ID which is different from UID of table A.

Sometimes table A tries to insert a duplicate entry which is denied by table B since it has a primary key.

The insert query is as below.

table A - CORE
table B - CRF

INSERT INTO CRF (CORE_UID,ACCT_NUM_MIN, ACCT_NUM_MAX,BIN, BUS_ID,BUS_NM,ISO_CTRY_CD, REGN_CD, PROD_TYPE_CD, CARD_TYPE)
(SELECT UID,LEFT(ACCT_NUM_MIN,16),LEFT( ACCT_NUM_MAX,16),BIN, BUS_ID,BUS_NM,ISO_CTRY_CD, REGN_CD, PROD_TYPE_CD, CARD_TYPE FROM CORE WHERE UID NOT IN (SELECT CORE_UID FROM CRF))

I am not worried about the performance. I have tried if not exists and except but since i am new i am not able to figure out the problem.

Thank you in advance for all who try to help me out.


i believe CONSTRAINT are still the best way to enforce your data integrity. you can also do this through trigger or check the table before insert, but i believe it'll be slower.|||

Quote:

Originally Posted by ck9663

i believe CONSTRAINT are still the best way to enforce your data integrity. you can also do this through trigger or check the table before insert, but i believe it'll be slower.


I AM NOT CONCERNED WITH THE PERFORMANCE... CAN YOU HELP ME WRITE THIS TRIGGER OR PROVIDE SYNTAX TO CHECK THE TABLE BEFORE THE INSERT?|||

Quote:

Originally Posted by desirocks

Hi all,

I am somewhat new to sql server. So help me in whatever way you can.

I have two tables one of which doesnt have a primary key(table A) and other has a composite key formed of two columns(table B). I need to insert entries in table B from table A(table A has a unique constratint UID which is passed to table B). Table B also has a unique ID which is different from UID of table A.

Sometimes table A tries to insert a duplicate entry which is denied by table B since it has a primary key.

The insert query is as below.

table A - CORE
table B - CRF

INSERT INTO CRF (CORE_UID,ACCT_NUM_MIN, ACCT_NUM_MAX,BIN, BUS_ID,BUS_NM,ISO_CTRY_CD, REGN_CD, PROD_TYPE_CD, CARD_TYPE)
(SELECT UID,LEFT(ACCT_NUM_MIN,16),LEFT( ACCT_NUM_MAX,16),BIN, BUS_ID,BUS_NM,ISO_CTRY_CD, REGN_CD, PROD_TYPE_CD, CARD_TYPE FROM CORE WHERE UID NOT IN (SELECT CORE_UID FROM CRF))

I am not worried about the performance. I have tried if not exists and except but since i am new i am not able to figure out the problem.

Thank you in advance for all who try to help me out.


Your above INSERT QUERY should work fine. What is the problem that you are facing here??|||

Quote:

Originally Posted by amitpatel66

Your above INSERT QUERY should work fine. What is the problem that you are facing here??


Since table CRF does have a composite key it doesnt allow duplicate entries and hence my application gives me an error. I want to some how check if the entry already exists or not and based upon the same i need to insert. Is there some way i can change the query ?|||It would help to know what column(s) comprise your primary key on table B. You say that A nad B have different unique columns, but your insert statement only limits duplicates based on table A's unique key. If table B has a different unique key then that is what you need to check fo in your not in statement. Also, a NOT EXISTS should be faster, but first you need to figure out what is unique on table B.|||OK, since you have said B has a composite key then you need something like this (I assumed that CORE_UID, BIN was the composite key, so adjust as needed):
INSERT INTO CRF (CORE_UID,
ACCT_NUM_MIN,
ACCT_NUM_MAX,
BIN,
BUS_ID,
BUS_NM,
ISO_CTRY_CD,
REGN_CD,
PROD_TYPE_CD,
CARD_TYPE)
SELECT UID,
LEFT(ACCT_NUM_MIN,16),
LEFT(ACCT_NUM_MAX,16),
BIN,
BUS_ID,
BUS_NM,
ISO_CTRY_CD,
REGN_CD,
PROD_TYPE_CD,
CARD_TYPE
FROM CORE A
WHERE NOT EXISTS (SELECT 1 FROM CRF B WHERE B.CORE_UID = A.UID and B.BIN = A.BIN)|||

Quote:

Originally Posted by rob313

OK, since you have said B has a composite key then you need something like this (I assumed that CORE_UID, BIN was the composite key, so adjust as needed):
INSERT INTO CRF (CORE_UID,
ACCT_NUM_MIN,
ACCT_NUM_MAX,
BIN,
BUS_ID,
BUS_NM,
ISO_CTRY_CD,
REGN_CD,
PROD_TYPE_CD,
CARD_TYPE)
SELECT UID,
LEFT(ACCT_NUM_MIN,16),
LEFT(ACCT_NUM_MAX,16),
BIN,
BUS_ID,
BUS_NM,
ISO_CTRY_CD,
REGN_CD,
PROD_TYPE_CD,
CARD_TYPE
FROM CORE A
WHERE NOT EXISTS (SELECT 1 FROM CRF B WHERE B.CORE_UID = A.UID and B.BIN = A.BIN)


hi rob,

The composite key comprises of ACCT_NUM_MIN and ACCT_NUM_MAX. The UID of CORE is being transferred to CRF and CRF also has its own UID. Can you specify what whould be the change in the query now?|||In that case, this should work:

INSERT INTO CRF (CORE_UID,
ACCT_NUM_MIN,
ACCT_NUM_MAX,
BIN,
BUS_ID,
BUS_NM,
ISO_CTRY_CD,
REGN_CD,
PROD_TYPE_CD,
CARD_TYPE)
SELECT UID,
LEFT(ACCT_NUM_MIN,16),
LEFT(ACCT_NUM_MAX,16),
BIN,
BUS_ID,
BUS_NM,
ISO_CTRY_CD,
REGN_CD,
PROD_TYPE_CD,
CARD_TYPE
FROM CORE A
WHERE NOT EXISTS (SELECT 1 FROM CRF B WHERE B.ACCT_NUM_MIN = A.LEFT(ACCT_NUM_MIN,16) and B.ACCT_NUM_MAX = A.LEFT(ACCT_NUM_MAX,16))

Sunday, February 19, 2012

avoid cursors.

hi what are ways of avoiding cursors?
my boss told me to use derived tables. any other tricks i can use?"ichor" <ichor@.hotmail.com> wrote in message
news:OyNAbw33FHA.3592@.TK2MSFTNGP12.phx.gbl...
> hi what are ways of avoiding cursors?
> my boss told me to use derived tables. any other tricks i can use?
>
It's usually a good idea to avoid cursors and mostly they are unnecessary.
For most data manipulation problems you can achieve the same thing as a
cursor by using set based code (code that operates on the entire set of data
at once rather than one row at a time). There is no single set based
technique to replace a cursor because cursors don't represent a particular
class of problem, they are just one tool with which to solve problems.
David Portas
SQL Server MVP
--|||Read up in SQL Server Books Online on topics like temporary tables,
subqueries, and use of the Case function. These can be used to replace
cursor based programming that performs complex row selection or updates.
"ichor" <ichor@.hotmail.com> wrote in message
news:OyNAbw33FHA.3592@.TK2MSFTNGP12.phx.gbl...
> hi what are ways of avoiding cursors?
> my boss told me to use derived tables. any other tricks i can use?
>

Avoid caching image datatype

Hi all,

I have a database with 40GB of binary objects stored in image columns
in two tables.

Our database server is also used for another 15 databases.

SQL Server caches the image-column, causing the cache-hit-ratio to
dramatically decrease, since fetching a couple of binaries from the
image-column uses up the
memory, and throws other cached objects out of the cache.

Is there a way, and could someone tell me how, to avoid caching of
specific
columns/tables?

Regards
///Magnus"Marwin" <alice@.underlandet.com> wrote in message
news:f2216085.0501270242.4da6d365@.posting.google.c om...
> Hi all,
> I have a database with 40GB of binary objects stored in image columns
> in two tables.
> Our database server is also used for another 15 databases.
> SQL Server caches the image-column, causing the cache-hit-ratio to
> dramatically decrease, since fetching a couple of binaries from the
> image-column uses up the
> memory, and throws other cached objects out of the cache.
> Is there a way, and could someone tell me how, to avoid caching of
> specific
> columns/tables?
> Regards
> ///Magnus

I don't believe there's any way to do this, and if MSSQL is caching the
image data rather than other data, then that suggests that the image data is
required more often anyway. By the way, when you say "cache hit ratio", do
you mean "buffer cache hit ratio"? The first relates to re-use of query
plans from the cache, the second is re-use of data pages, so that's the one
you want to watch. If the buffer cache hit ratio is low, the best option is
probably to add more memory to your server, assuming that you're seeing a
significant performance hit.

Simon|||There is no way to do that on specific tables. SQL Server internally
implemented a sophisticated page replacement algorithm that keeps the most
frequently used data page in memory. So the hot frequently accessed
objects/pages will not be kicked out of cache just because some apps read a
lot of image data.

--
Gang He
Software Design Engineer
Microsoft SQL Server Storage Engine

This posting is provided "AS IS" with no warranties, and confers no rights.
"Marwin" <alice@.underlandet.com> wrote in message
news:f2216085.0501270242.4da6d365@.posting.google.c om...
> Hi all,
> I have a database with 40GB of binary objects stored in image columns
> in two tables.
> Our database server is also used for another 15 databases.
> SQL Server caches the image-column, causing the cache-hit-ratio to
> dramatically decrease, since fetching a couple of binaries from the
> image-column uses up the
> memory, and throws other cached objects out of the cache.
> Is there a way, and could someone tell me how, to avoid caching of
> specific
> columns/tables?
> Regards
> ///Magnus

Sunday, February 12, 2012

AUTOSTATS on tempdb

Hello,

I've found this recomendation recently:

"To help speed queries or joins on large temp tables, be sure the AUTOSTATS database option is turned on for tempdb"

How can I find out what this option set for our tempdb? If I go to "database options" of tempdb under "System databases", I do not see that option at all.

Please help. Thanks

Do you see 'Auto Create Statistics'?

Alternatively you can run sp_dboption on tempdb to see what the settings are:

exec sp_dboption 'tempdb'

You should see that 'auto create statistics' is set

Don

|||

run this query

select DATABASEPROPERTY( 'tempdb ', 'IsAutoCreateStatistics' )

1 = TRUE

0 = FALSE

NULL = Invalid input

Madhu

Friday, February 10, 2012

Autonumber when inserting

Hi i have a problem that i can′t solve!
I have three tables last_id, customer, prospect. The "last_id" table have
one colum that stores the last used id number (int), customer tables stores
info about the customer and a "last_id" number and the prospect stores new
customers!
I want to make an insert into customer table from the prospect table and ad
one extra column with the last_id number +1
insert into customer(name,adress,l_id)
select p_name,p_adress,l_id
from prospect,last_id
i don′t want alter the table and use identity
i can make i work with a cursor, is there any other way to do it'Hi
CREATE TABLE #Test
(
col INT
)
INSERT INTO #Test VALUES (1)
GO
INSERT INTO #Test SELECT COALESCE(MAX(col),0)+1 FROM #Test
GO
SELECT * FROM #test
DROP TABLE #Test
"LeSurfer" <LeSurfer@.discussions.microsoft.com> wrote in message
news:8C74D3BF-12D2-4FED-B07D-5070867310B4@.microsoft.com...
> Hi i have a problem that i cant solve!
> I have three tables last_id, customer, prospect. The "last_id" table have
> one colum that stores the last used id number (int), customer tables
stores
> info about the customer and a "last_id" number and the prospect stores new
> customers!
> I want to make an insert into customer table from the prospect table and
ad
> one extra column with the last_id number +1
> insert into customer(name,adress,l_id)
> select p_name,p_adress,l_id
> from prospect,last_id
> i dont want alter the table and use identity
> i can make i work with a cursor, is there any other way to do it'
>|||I couldn′t make it work, i tried this
select coalesce(max(l_id),0)+1,name
from ct_contact cross join kundfil
this is the error message:
Server: Msg 8118, Level 16, State 1, Line 1
Column 'Kundfil.kund' is invalid in the select list because it is not
contained in an aggregate function and there is no GROUP BY clause.
any ideas on what i could try'
"Uri Dimant" wrote:

> Hi
> CREATE TABLE #Test
> (
> col INT
> )
> INSERT INTO #Test VALUES (1)
> GO
> INSERT INTO #Test SELECT COALESCE(MAX(col),0)+1 FROM #Test
> GO
> SELECT * FROM #test
> DROP TABLE #Test
>
> "LeSurfer" <LeSurfer@.discussions.microsoft.com> wrote in message
> news:8C74D3BF-12D2-4FED-B07D-5070867310B4@.microsoft.com...
> stores
> ad
>
>|||Hi
Add this column 'Kundfil.kund' into GROUP BY clause
"LeSurfer" <LeSurfer@.discussions.microsoft.com> wrote in message
news:D5A79FA4-6E93-4EB7-870D-F754B26DF287@.microsoft.com...
> I couldnt make it work, i tried this
> select coalesce(max(l_id),0)+1,name
> from ct_contact cross join kundfil
> this is the error message:
> Server: Msg 8118, Level 16, State 1, Line 1
> Column 'Kundfil.kund' is invalid in the select list because it is not
> contained in an aggregate function and there is no GROUP BY clause.
> any ideas on what i could try'
> "Uri Dimant" wrote:
>
have
new
and

AutoNumber Primary Key

We are testing the upload of an Access 2002 data database to SQL Server
2000. Some of the tables had AutoNumber fields that were Random (due to it
being a replicated table in Access). The upload created triggers similar to
the example below. However, Access applications fail on insert because the
primary key field is not populated until the trigger runs. Any help is
appreciated.
CREATE TRIGGER T_AssessmentDocs_ITrig ON dbo.AssessmentDocs FOR INSERT AS
SET NOCOUNT ON
DECLARE @.randc int, @.newc int /* FOR AUTONUMBER-EMULATION CODE */
/* * RANDOM AUTONUMBER EMULATION CODE FOR FIELD 'DocID' */
SELECT @.randc = (SELECT convert(int, rand() * power(2, 30)))
SELECT @.newc = (SELECT DocID FROM inserted)
UPDATE AssessmentDocs SET DocID = @.randc WHERE DocID = @.newc
DavidDavid C wrote:
> We are testing the upload of an Access 2002 data database to SQL
> Server 2000. Some of the tables had AutoNumber fields that were
> Random (due to it being a replicated table in Access). The upload
> created triggers similar to the example below. However, Access
> applications fail on insert because the primary key field is not
> populated until the trigger runs. Any help is appreciated.
> CREATE TRIGGER T_AssessmentDocs_ITrig ON dbo.AssessmentDocs FOR
> INSERT AS SET NOCOUNT ON
> DECLARE @.randc int, @.newc int /* FOR AUTONUMBER-EMULATION CODE */
> /* * RANDOM AUTONUMBER EMULATION CODE FOR FIELD 'DocID' */
> SELECT @.randc = (SELECT convert(int, rand() * power(2, 30)))
> SELECT @.newc = (SELECT DocID FROM inserted)
> UPDATE AssessmentDocs SET DocID = @.randc WHERE DocID = @.newc
> David
Change the column to an IDENTITY value. SQL Server automates the
generation of the next value. You may have to seed the identity value
using the MAX(ID) + 1 in the table.
You can pull back the newly inserted identity value into the application
using the SCOPE_IDENTITY() function.
David Gugick
Imceda Software
www.imceda.com|||David Gugick wrote:
> David C wrote:
>> We are testing the upload of an Access 2002 data database to SQL
>> Server 2000. Some of the tables had AutoNumber fields that were
>> Random (due to it being a replicated table in Access). The upload
>> created triggers similar to the example below. However, Access
>> applications fail on insert because the primary key field is not
>> populated until the trigger runs. Any help is appreciated.
>> CREATE TRIGGER T_AssessmentDocs_ITrig ON dbo.AssessmentDocs FOR
>> INSERT AS SET NOCOUNT ON
>> DECLARE @.randc int, @.newc int /* FOR AUTONUMBER-EMULATION CODE */
>> /* * RANDOM AUTONUMBER EMULATION CODE FOR FIELD 'DocID' */
>> SELECT @.randc = (SELECT convert(int, rand() * power(2, 30)))
>> SELECT @.newc = (SELECT DocID FROM inserted)
>> UPDATE AssessmentDocs SET DocID = @.randc WHERE DocID = @.newc
>> David
> Change the column to an IDENTITY value. SQL Server automates the
> generation of the next value. You may have to seed the identity value
> using the MAX(ID) + 1 in the table.
Or by using DBCC CHECKIDENT, which should do the same job.

AutoNumber Primary Key

We are testing the upload of an Access 2002 data database to SQL Server
2000. Some of the tables had AutoNumber fields that were Random (due to it
being a replicated table in Access). The upload created triggers similar to
the example below. However, Access applications fail on insert because the
primary key field is not populated until the trigger runs. Any help is
appreciated.
CREATE TRIGGER T_AssessmentDocs_ITrig ON dbo.AssessmentDocs FOR INSERT AS
SET NOCOUNT ON
DECLARE @.randc int, @.newc int /* FOR AUTONUMBER-EMULATION CODE */
/* * RANDOM AUTONUMBER EMULATION CODE FOR FIELD 'DocID' */
SELECT @.randc = (SELECT convert(int, rand() * power(2, 30)))
SELECT @.newc = (SELECT DocID FROM inserted)
UPDATE AssessmentDocs SET DocID = @.randc WHERE DocID = @.newc
David
David C wrote:
> We are testing the upload of an Access 2002 data database to SQL
> Server 2000. Some of the tables had AutoNumber fields that were
> Random (due to it being a replicated table in Access). The upload
> created triggers similar to the example below. However, Access
> applications fail on insert because the primary key field is not
> populated until the trigger runs. Any help is appreciated.
> CREATE TRIGGER T_AssessmentDocs_ITrig ON dbo.AssessmentDocs FOR
> INSERT AS SET NOCOUNT ON
> DECLARE @.randc int, @.newc int /* FOR AUTONUMBER-EMULATION CODE */
> /* * RANDOM AUTONUMBER EMULATION CODE FOR FIELD 'DocID' */
> SELECT @.randc = (SELECT convert(int, rand() * power(2, 30)))
> SELECT @.newc = (SELECT DocID FROM inserted)
> UPDATE AssessmentDocs SET DocID = @.randc WHERE DocID = @.newc
> David
Change the column to an IDENTITY value. SQL Server automates the
generation of the next value. You may have to seed the identity value
using the MAX(ID) + 1 in the table.
You can pull back the newly inserted identity value into the application
using the SCOPE_IDENTITY() function.
David Gugick
Imceda Software
www.imceda.com
|||David Gugick wrote:
> David C wrote:
> Change the column to an IDENTITY value. SQL Server automates the
> generation of the next value. You may have to seed the identity value
> using the MAX(ID) + 1 in the table.
Or by using DBCC CHECKIDENT, which should do the same job.

AutoNumber Primary Key

We are testing the upload of an Access 2002 data database to SQL Server
2000. Some of the tables had AutoNumber fields that were Random (due to it
being a replicated table in Access). The upload created triggers similar to
the example below. However, Access applications fail on insert because the
primary key field is not populated until the trigger runs. Any help is
appreciated.
CREATE TRIGGER T_AssessmentDocs_ITrig ON dbo.AssessmentDocs FOR INSERT AS
SET NOCOUNT ON
DECLARE @.randc int, @.newc int /* FOR AUTONUMBER-EMULATION CODE */
/* * RANDOM AUTONUMBER EMULATION CODE FOR FIELD 'DocID' */
SELECT @.randc = (SELECT convert(int, rand() * power(2, 30)))
SELECT @.newc = (SELECT DocID FROM inserted)
UPDATE AssessmentDocs SET DocID = @.randc WHERE DocID = @.newc
DavidDavid C wrote:
> We are testing the upload of an Access 2002 data database to SQL
> Server 2000. Some of the tables had AutoNumber fields that were
> Random (due to it being a replicated table in Access). The upload
> created triggers similar to the example below. However, Access
> applications fail on insert because the primary key field is not
> populated until the trigger runs. Any help is appreciated.
> CREATE TRIGGER T_AssessmentDocs_ITrig ON dbo.AssessmentDocs FOR
> INSERT AS SET NOCOUNT ON
> DECLARE @.randc int, @.newc int /* FOR AUTONUMBER-EMULATION CODE */
> /* * RANDOM AUTONUMBER EMULATION CODE FOR FIELD 'DocID' */
> SELECT @.randc = (SELECT convert(int, rand() * power(2, 30)))
> SELECT @.newc = (SELECT DocID FROM inserted)
> UPDATE AssessmentDocs SET DocID = @.randc WHERE DocID = @.newc
> David
Change the column to an IDENTITY value. SQL Server automates the
generation of the next value. You may have to seed the identity value
using the MAX(ID) + 1 in the table.
You can pull back the newly inserted identity value into the application
using the SCOPE_IDENTITY() function.
David Gugick
Imceda Software
www.imceda.com|||David Gugick wrote:
> David C wrote:
> Change the column to an IDENTITY value. SQL Server automates the
> generation of the next value. You may have to seed the identity value
> using the MAX(ID) + 1 in the table.
Or by using DBCC CHECKIDENT, which should do the same job.

AutoNumber PK on Most Tables?

I tend to create a "RecordID" IDENTITY-driven PK on subtables where I think most
people wouldn't because there is no immediate obvious need.
My reasoning is "why not?"...seems like a significant convenience later on when
faced with unexpected situations that require identification of specific
records...and the cost seems minimal.
Comments?
--
PeteCresswellI agree with you. I do the same too, which is to create a PK for all tables.
But one thing that I watch for is to see the query patterns on these tables
so that I can use clustered indexes on them. PK's are automatically created
as clustered (unless the nonclustered is specified). For example, if my
subtable resulted because of normalization, I would rather put a clustered
index on the FK and not on the PK.
--
HTH,
SriSamp
Please reply to the whole group only!
http://www32.brinkster.com/srisamp
"(Pete Cresswell)" <x@.y.z> wrote in message
news:3apnsvcaak4cvco9re9vr7h7tuu06384v9@.4ax.com...
> I tend to create a "RecordID" IDENTITY-driven PK on subtables where I
think most
> people wouldn't because there is no immediate obvious need.
> My reasoning is "why not?"...seems like a significant convenience later
on when
> faced with unexpected situations that require identification of specific
> records...and the cost seems minimal.
> Comments?
> --
> PeteCresswell

Autonumber equivalent in SQL Server?

Hi

So far, I have only used Access which has an autonumber data type so that in some of my tables the id is automatically generated.
I guess this is a simple question but is there an equivalent data type in sql server?

Thanks in advance.Yes - Int with an Identity property set to Yes