|
What is the best way to write an SQL stored procedure: by using temporary tables (e.g #tmpSomething or @tmpSomething) or by using multiple selects? How is this affecting the speed of execution and the memory usage? Thank you in advance.
I am fighting against the Universe...
Reference-Rick Cook
|
|
|
|
|
Albu Marius wrote: What is the best way to write an SQL stored procedure: by using temporary tables (e.g #tmpSomething or @tmpSomething) or by using multiple selects? How is this affecting the speed of execution and the memory usage? Thank you in advance.
It depends on what query you are running. There is no simple answer. Experiment, see what is better. Then 6 months later when your database schema has changed expect the performance metrics to have changed that your initial findings no longer hold true.
|
|
|
|
|
I'm sorry, I should have been more specific in my post. The situation : 2 selects (not very large)each returning about 3000 records . One select is saved in a temporary table (#tmpSomething) and the other one is joined with the first and the result is outputed (duration - few miliseconds). I was advised to never use temporary tables because of the performance issues. I know this is the case of @ tables which store all the data in memory and for a large number of records (tens of thousands) the SQL Server hangs for a time before returning the result. But my problem is why can 't I use # table (which are stored on disk)? Is the impact to performance so big due to the I/O operations? As I said I was advised to always use a select contained in the bigger select instead of temporary tables. This seems to me as a bad practice because this way the SQL code if pretty hard to understand and I'm not sure this resolves the performance issues. If someone can shed some light on this dilema of mine , please do.
I am fighting against the Universe...
Reference-Rick Cook
|
|
|
|
|
Albu Marius wrote: I was advised to never use temporary tables because of the performance issues.
That is rubbish. A few years ago I went from taking 20 minutes for a query down to 7 seconds by introducing a couple of temporary tables. (The source table was growing at a gigabyte per week, so there was A LOT of data in there)
Albu Marius wrote: I know this is the case of @ tables which store all the data in memory and for a large number of records (tens of thousands) the SQL Server hangs for a time before returning the result
That is also incorrect. Table variables (or @ tables as you put it) are just anothe form of temp table. They will get flushed to the TEMPDB if it needs to, but typically you'd use them in situations where the dataset was small enough that it wouldn't go there.
Albu Marius wrote: But my problem is why can 't I use # table (which are stored on disk)?
Again, incorrect. If they are small enough and used quickly enough they will be created, populated and dropped without needing to be flushed to disk. Of course the log will still be updated.
Albu Marius wrote: Is the impact to performance so big due to the I/O operations?
Seriously, you need to experiment. There is so much going on that you can't know otherwise. I don't know, you don't know and anyone that says they do know is lying.
The myths you have picked up are a result of someone at ONE time seeing that it operates in a particular way. The next query with the same technique make operate differently because the query optimiser recons it will be better.
Albu Marius wrote: As I said I was advised to always use a select contained in the bigger select instead of temporary tables. This seems to me as a bad practice because this way the SQL code if pretty hard to understand and I'm not sure this resolves the performance issues. If someone can shed some light on this dilema of mine , please do.
You are right it is bad practice. But the bad practice is to have a rule that doesn't take in to account the current environment your query is operating in.
Temp tables can improve performance (as I demonstrated above) and quite significantly. They can also reduce performance. Which is why you need to experiment with different ways of doing things.
|
|
|
|
|
Thank you for your advice. Your explanations cleared this issue for me.
I am fighting against the Universe...
Reference-Rick Cook
|
|
|
|
|
how can i block the data in Data base (SQL server 2000)
by variable in the data base like Year
please some definition and detailes
ahmed eldeghedy
|
|
|
|
|
ahmed eldeghedy wrote: please some definition and detailes
Yes, that would be nice.
ahmed eldeghedy wrote: how can i block the data in Data base (SQL server 2000)
by variable in the data base like Year
What do you mean by "block"?
Do you mean filter out?
SELECT * FROM SomeTable<br />
WHERE Year = 2007
|
|
|
|
|
Colin Angus Mackay wrote: What do you mean by "block"?
What does he mean?
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
Do not email me - If I respond it is for everyones benefit, not just yours. Other people make have the same problem and are searching the forums for an answer, if I email a response those people will not benefit.
ahmed eldeghedy wrote: what i mean
when i take instans form the object (tabel database)
i whant the data bases retrun table by the data not block
not filter
ahmed eldeghedy
That still does not make sense. What do you mean by "block"? How do you want data blocked? What sort of data do you want blocked?
Perhaps you want to revoke access to the tables and have everything done through views and grant access to the views based on the role the user has. Perhaps you want to do everything through stored procedures which will only do what the stored procedure has been programmed to do and nothing more. But I just don't know from your description.
|
|
|
|
|
Colin Angus Mackay wrote: That still does not make sense.
It certainly still makes no sense
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
Hello Everybody
Table Name dbo.Books where I kept Books Information. At First You need to put All information on dbo.Books table. Now this book is not Active because I need to make a Copy of this book which will be stored at dbo.CopyBooks table.
dbo.Books table
ISBN BookName
1001 Sql Server 2000
1002 Sql Server 2005
Now I want to make Copy of those book
dbo.CopyBooks
ISBN Book_ID
1001 1001101
1001 1001102
1001 1001103
1002 1002101
1002 1002102
1002 1002103
1002 1002104
First four digit is ISBN number and last three digit indicate book copy number 101 is the First Copy, 102 is the Second Copy.
How will I do it?
Thanks
Sarfarj Ahmed
|
|
|
|
|
You could add another column to CopyBooks called CopyID. This would be a sequence no. uniquely identifying a copy of each book. For example:
ISBN CopyID Book_ID
1001 101 1001101
1001 102 1001102
1001 103 1001103
1002 101 1002101
1002 102 1002102
1002 103 1002103
1002 104 1002104
Book_ID could then be changed to a computed column using the formula (CONVERT([varchar],[ISBN],0)+CONVERT([varchar],[CopyID],0)) .
To find the next CopyID for a particular ISBN, all you have to do is find the current highest CopyID for that ISBN and add 1.
Paul Marfleet
|
|
|
|
|
Thanks
Basically wot I would like to is....
table dbo.copyBook
AutoID ISBN CopyID BookID
1 1001 101 1001101
2 1001 102 1001102
3 1002 101 1002101
4 1002 102 1002102
On my frmAddCopy.vb
When You click btnAddCopy then only ISBN will send to database and return BookID. Also How will I incresed copyID in database. Please tell me more information. I dont know how to use function and procedure in SQL SERVER 2005 dataBase.
Is their any thing that i can do it in sql server 2005 database?
Sarfarj Ahmed
|
|
|
|
|
If you don't know how to use functions or procedures in SQL Server, you should buy a book and start learning. I don't think I can help you much if you don't have basic knowledge of the tools you are using.
If you aren't familiar with the SQL language, this[^] may be a good place to start. Look at using the MAX aggregate function to help you increment the CopyID sequence.
Paul Marfleet
|
|
|
|
|
about dbo.fine
AutoID MemberID ReturnDate ReturnOn Fine
1 S10001 23/10/2007 25/10/2007 1.00
2 S10002 21/10/2007 25/10/2007 2.00
3 S10003 21/10/2007 2.00
I wolud like create this table like this,
on dbo.fine table will automatically check ReturnDate column from dbo.borrowBook table add fine on Fine.Fine table. It will will be check CurrentDate and ReturnDate.
How will I create this Table? Any Instruction.......
Sarfarj Ahmed
|
|
|
|
|
I don't understand what your question is. Do you want help creating the table? Use Management Studio or the CREATE TABLE DDL statement.
Paul Marfleet
|
|
|
|
|
Yes I want to create table which will check coloum value from other table like dbo.fine table will check returndate from dbo.borrow table.
Like if ReturnDate is 20/12/2006 but SubmitedDate is 25/12/2006 . Per day fine 20P, so fine coloum On dbo.Fine table will automatically add £1.00
How CAN I DO IT?
Sarfarj Ahmed
|
|
|
|
|
First of all, don't shout. It's rude. This is a free site, so be grateful that people are willing to give up their time to help you.
Secondly, this is pretty basic arithmatic isn't it? Calculate the number of days difference between the ReturnDate and SubmittedDate using the DATEDIFF function and multiply the result by the daily fine to get the overall fine.
Paul Marfleet
|
|
|
|
|
we have two database A & B.i want to update field Address1,Address2,Address3 in database A with Address1,Address2,Address3 in B database.how can achived it.
Thanks
|
|
|
|
|
update a.dbo.TableName .....etc
not both databases should be in same server.,
|
|
|
|
|
Something like:
update
A.dbo.Address1 X
set
X.Address1 = Y.Address1,
X.Address2 = Y.Address2,
X.Address3 = Y.Address3
from
B.dbo.Address Y
where
X.Id = Y.Id
As stated before, both databases need to be on the same server for this to work.
Regards,
Rob Philpott.
|
|
|
|
|
i am using an oledbcommand object to perform both insert and update based on the command text supplied and i am adding the parameters for update also in the order of insert. So incase of update though, its not giving any error/exception but database is not getting updated.
Can any body pls tell me how to resolve this or any other way to implement both the functionalities using single method.
Thanks in advance.
|
|
|
|
|
The order of parameters should not be an issue. You need to first check your update stored procedures or statements. The easiest way to do this is to trying and execute them (using EXEC) directly in the database. This will enable you confirm whether they are OK.
|
|
|
|
|
I Have a table of emp with the fields of
Empno empname Empsalary
1 sam 1000
2 joe 2000
3 harry 3000
Now i want a query for a give me the result like this
Empno empname Empsal salTotal
1 sam 1000 1000
2 joe 2000 3000(sum of previous salary Total with the current sal)
3 harry 3000 6000 sum of previous salary Total with the current sal
How can be achive this result by using query
Thanks a ton in advance
Thanks and Regards
Aavesh Agarwal
|
|
|
|
|
This will give you what you are after, but will take a long time to run if your table is large, as it executes the subquery once for each line.
select EmpNo, EmpName,
(select sum(EmpSalary) from emp e2 where e2.EmpNo <= e1.EmpNo)
from emp e1
order by e1.EmpNo
Hope that helps!!
-------------------------------------------
Don't walk in front of me, I may not follow;
Don't walk behind me, I may not lead;
Just bugger off and leave me alone!!
|
|
|
|