Optimize performance for date range query in SQL - sql

I have scenario as below: I have a table Tblbalance which contains a date and time column.
When I search using normal between clause in my stored procedure, it will throw error timeout in linq.
I tried to extend default time from 30 sec to 2 min in linq but not worked.
Please help me to get fastest way to return from select query
Sample query I tried is as below:
select
col1,
col2,
col3,
(select top 1 col from tblname where id = tblbalance.id)
from
tblbalance
where
datecol between startdate and todate
order by
col3

I would suggest to include one more column in you table and copy numeric of datetime in that column, Also create a index on the newly created numeric column. Then instead of using the datetime column in where clause, try using numeric column.
This way your query will run on index and will be optimised.

Related

Using an UDF to query a table in Hive

I have the following UDF available on Hive to convert a time bigint to date,
to_date(from_utc_timestamp(from_unixtime(cast(listed_time/1000 AS bigint)),'PST'))
I want to use this UDF to query a table on a specific date. Something like,
SELECT * FROM <table_name>
WHERE date = '2020-03-01'
ORDER BY <something>
LIMIT 10
I would suggest to change the logic: avoid applying the function to the column being filtered, because it is an inefficient approach. The function needs to be invoked for every row, which prevents the query from benefiting an index.
On the other hand, you can simply convert the input date to a unix timestamp (possibly with an UDF). This should look like;
SELECT * FROM <table_name>
WHERE date = to_utc_timestamp('2020-03-01', 'PST') * 1000
ORDER BY <something>
LIMIT 10

Strange behaviour of SQL Server query

I have a query in a validation stored procedure. It goes something like this:
SELECT *
FROM Table1
WHERE batchID IN (SELECT id FROM #tempIds)
AND CAST(field1 AS DATE) > CAST(field2 AS DATE)
Both field1 and field2 have valid dates, i.e. doing IdDate on field1/2 returns 1.
The #tempIds table only has one column ID and contains only one row.
When I run the above query, I get this error:
Unable to convert varchar to date
But instead of selecting batch ids from temp table if I put hard-coded ID from the same temp table it works.
Any ideas what could be the issue?
The problem is that you are using varchar to store date (or datetime) values.
Choosing the correct data type for your columns would save you from a lot of problems, this one included. For detailed information, read Aaron Bertrand's Bad habits to kick : choosing the wrong data type.
Now, to address our conversation in the comments - SQL Server does not guarantee the order on which the conditions in the where clause are evaluated. This means that even if all the "date" strings in both your columns are convertible to date values for the specific batchID, you only need one wrong value in one of the columns to raise the "Unable to convert varchar to date" error.
This also means that even if you where to write your query the way Larry B suggested in his answer (now deleted - so for the sake of future readers - this was his suggestion:)
SELECT *
FROM Table1
WHERE batchID IN (SELECT id FROM #tempIds)
AND ISDATE(field1) = 1
AND ISDATE(field2) = 1
AND CAST(field1 AS DATE) > CAST(field2 AS DATE)
There is no guarantee that the last condition (cast(field1 as date) > cast(field2 as date)) will be evaluated after the isdate(field1)=1 condition.
The correct thing to do is to fix the problem - change the data types of the columns to the correct data type.
Assuming that can't be done (if you have no control over the structure of the database, for instance) - you can do a couple of things:
Find all the places where the values in field1 and in field2 can't be converted to dates and fix them. Consider adding a check constraint to validate that the values of these columns can be converted to date (assuming you can).
Separate your query into 2 parts:
;With cte as
(
SELECT *
FROM Table1
WHERE batchID IN (SELECT id FROM #tempIds)
AND ISDATE(field1) = 1
AND ISDATE(field2) = 1
)
SELECT *
FROM cte
WHERE CAST(field1 AS DATE) > CAST(field2 AS DATE)
This will eliminate the error, since you are only casting values where the ISDATE function already returned 1, but might not return some rows you want back, if the value if either field1 or field2 is wrong in these rows.

Force SQL Server to Evaluate Sub Query First

How can I force SQL Server to Evaluate a sub query first?
My query looks something like:
SELECT ObjectId FROM
(SELECT ObjectId FROM Table Where Id = #Id) T
WHERE fn_LongRunningFunction(T.ObjectId) = 1
I want the outer where clause to evaluate on the result of the inner query. How would I do this without inserting the sub query into a temp table.
When I execute this query SQL evaluates the query as if it where written like:
SELECT ObjectId FROM Table Where Id = #Id AND fn_LongRunningFunction(ObjectId) = 1
which is not what I want.
Why you're even using a sub-query here you could have simply used one query here something like ....
SELECT ObjectId
FROM Table
Where Id = #Id
AND fn_LongRunningFunction(ObjectId) = 1
Note
Using a scalar function in where clause as you have will cause a full table scan, Since sql server has to touch every row in the column and execute the function on ObjectId column values to evaluate if it is equal to 1 or not.
Avoid using any functions in where clauses on column names whenever possible.
for example if you are evaluating a value of a column against a given value do the reverse on the other side of the comparison operator and leave the column alone in where clause, for example if you are looking for values in a table
WHERE ColumnName + 20 < 100
Instead of doing you could do something like
WHERE ColumnName < 100 -20
In first example sql server will have to touch every row and will add 20 to its value to evaluate it against 100 which will cause a table scan.
In 2nd example if sql server has an index on that column it will simply do a seek to see which values are less then 100 -20.

SQL select query with one or two column excluded

how to write a select query with one or two column excluded instead of list down required columns.
for example we have a table Table1 with 10 columns (col_1,col_2,col_2 ..... col_10)
and we just want select col_1 to col_8.
instead of writing like
Select col_1, col_2, col_3 .... col_8 from Table1
should we select like this
Select -(col_9, col_10) * from Table1
This is possible but you would have to use dynamic SQL or a stored procedure. It would still mean having to specify the start and end column using fixed column names and indexes and if the columns weren't in running order (colN, colN+1, etc.) then it would become messy.
So I suppose your answer it, just type it out. The benefit you will gain from doing something cleaver is small.
I hope this helps.
Any ideas?
I think we can select column name first then write into query.
1 Select column with excluded.
SHOW COLUMNS FROM _table WHERE FIELD NOT IN ('column1','column2')
2 Then, Use the column result we've got from above query to write to select query column.
SELECT {result_aboved} FROM _table WHERE 1
but I don't think it can be only once query for this case. you have to query twice times at least.

SQL Server 2005 - Add Milliseconds column onto DateTime column

How do i combine 2 column using SQL server 2005 ?
Problem is that The DateTime is stored in 1 column and the milliseconds is stored in another column.
I want to add the Milliseconds onto DateTime column to give it a more accurate DateTime.
I need to use this DateTime to query record accurate to milliseconds.
Any idea?
I need to replace DateTime with the added values.
SELECT *
FROM TABLENAME
WHERE [DateTime] >= '2011-04-12 12:00:00 AM'
AND [DateTime] <= '2011-05-25 3:35:04 AM'
and run the query.
Well, first solve your problem:
SELECT DATEADD(millisecond,<milliscolumn>,<datetimecolumn>) from <table>
And then file a bug report that these should just be stored in one column anyway.
You can do this, based on your sample query, but note that this destroys the possibility of the server being able to use an index:
SELECT * FROM TABLENAME WHERE
DATEADD(millisecond,[MillisecondColumn],[DateTime]) between
'2011-04-12T12:00:00' AND '2011-05-25T03:35:04'
If this is a large table, then indexes may be important. If you can't alter whatever's populating this data, you might want to add this calculation as a persisted computed column to this table, and then index and query against that.
Note that I've replaced your two comparisons with a single BETWEEN, and also adjusted the datetime strings so that they're not affected by regional settings.
SELECT (ColumnA + ColumnB) AS ColumnZ
FROM Table
might solve your problem.