sql-server-2005

LEFT JOIN Significantly faster than INNER JOIN

对着背影说爱祢 提交于 2020-01-01 01:16:08
问题 I have a table ( MainTable ) with a bit over 600,000 records. It joins onto itself via a 2nd table ( JoinTable ) in a parent/child type relationship: SELECT Child.ID, Parent.ID FROM MainTable AS Child JOIN JoinTable ON Child.ID = JoinTable.ID JOIN MainTable AS Parent ON Parent.ID = JoinTable.ParentID AND Parent.SomeOtherData = Child.SomeOtherData I know that every child record has a parent record and the data in JoinTable is acurate. When I run this query it takes literally minutes to run.

Changing INT to BigInt

旧巷老猫 提交于 2019-12-31 17:50:20
问题 I have a warehouse table with 16 tons of data in it. I have a few Integer columns in it. We have to cast these into BIGINT for every query we write, because the SUM is too large to fit in an INT. We now have a new datamart under development. So we thought, why not change all these columns into BIGINT and we have less to worry for the new set of queries. Since the data is already loaded, I figured I would use Management Studio and change the data type. But I first get a warning: Saving

Why do multiple WHERE conditions slow query rather than speed it up?

只愿长相守 提交于 2019-12-31 13:55:46
问题 The problem is that the query in question runs very slow when compared to the query run with one or two, rather than all three of its conditions. Now the query. Select Count(*) From SearchTable Where [Date] >= '8/1/2009' AND [Zip] In (Select ZipCode from dbo.ZipCodesForRadius('30348', 150)) AND FreeText([Description], 'keyword list here') The first condition is self explanatory. The second uses a UDF to get a list of Zip Codes within 150 miles of 30348. The third uses a full text index to

Why do multiple WHERE conditions slow query rather than speed it up?

左心房为你撑大大i 提交于 2019-12-31 13:53:59
问题 The problem is that the query in question runs very slow when compared to the query run with one or two, rather than all three of its conditions. Now the query. Select Count(*) From SearchTable Where [Date] >= '8/1/2009' AND [Zip] In (Select ZipCode from dbo.ZipCodesForRadius('30348', 150)) AND FreeText([Description], 'keyword list here') The first condition is self explanatory. The second uses a UDF to get a list of Zip Codes within 150 miles of 30348. The third uses a full text index to

Deletion of duplicate records using one query only

こ雲淡風輕ζ 提交于 2019-12-31 10:04:42
问题 I am using SQL server 2005. I have a table like this - ID Name 1 a 1 a 1 a 2 b 2 b 3 c 4 d 4 d In this, I want to delete all duplicate entries and retain only one instance as - ID Name 1 a 2 b 3 c 4 d I can do this easily by adding another identity column to this table and having unique numbers in it and then deleting the duplicate records. However I want to know if I can delete the duplicate records without adding that additional column to this table. Additionally if this can be done using

Useful system stored procedures in SQL Server

烈酒焚心 提交于 2019-12-31 08:15:01
问题 I recently discovered that I could use the sp_help to get a table definition and have been hooked onto it since then. Before my discovery, I had to open up the Object explorer in SQL Management studio, manually search for the table name, right click on the table and select Design. That was a lot of effort! What other system stored procedures do you all use that you can't simply live without? 回答1: Alt + F1 is a good shortcut key for sp_help . sp_helptext is another goodie for getting stored

SQL: Where MYID = ANY?

孤者浪人 提交于 2019-12-31 07:39:05
问题 In a SQL query, that contains ... WHERE MYID = @1 .... I have to manage 2 cases 1) There is a filter on a column, @1 will be a number (1,2,X...) 2) There is no filter on that column, @1 will be ...? ( ANY ) Is there something for this "any" (SQL Server 2005) ? PS. Obviously, I understand that I can remove the "where". PPS. I explain myself for better understanding: I have this query in the code, and would like to pass an integer as parameter when the filter is ON, and "something" when my

Convert smallint to time

牧云@^-^@ 提交于 2019-12-31 05:29:13
问题 I am trying to convert a smallint to a time format. I am using SQL Server 2005 which doesn't support the time datatype. I am storing the time as 1400 which is a smallint . When retrieving I want it to be converted to a time format. Such as 14:00 Any ideas or guidance on the matter. If there is an easier way to do it or if the way I am trying is possible? 回答1: You can get a result as varchar by using this: SELECT RIGHT('0' + CONVERT(varchar(10), yourTime / 100), 2) + ':' + RIGHT('0' + CONVERT

default concurrency control for sql server 2005

冷暖自知 提交于 2019-12-31 04:09:05
问题 what is the default concurrency control for SQL Server 2005? Is it optimistic concurrency control or pessimistic concurrency control? Can this be set? Thanks in anticipation 回答1: "Pessimistic" seems to be the default in 2005, although snapshot isolation can be activated if desired: In the default pessimistic model, the first writer will block all subsequent writers, but using SI, subsequent writers could actually receive error messages and the application would need to resubmit the original

Splitting of comma separated values [duplicate]

纵然是瞬间 提交于 2019-12-31 03:43:04
问题 This question already has answers here : T-SQL: Opposite to string concatenation - how to split string into multiple records [duplicate] (11 answers) Closed 5 years ago . I have some columns with comma-separated values. For example A as,ad,af,ag My manager wants the output looks like this: A as ad af ag All these should come under one row. I guess we need to use something line break. Hi can we use replace with char(13)+char(10) something. This one also works... Thanks, Shashra 回答1: You can