Share via

How to create the partition dynamically using SP

Arutprakasam 261 Reputation points
2023-11-24T23:42:24.9233333+00:00

Hi, can any share the query partition the table based on timestamp. My scenario is I have to create the partition for everyday based on date and timestamp. Please help me.

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


Answer accepted by question author

Erland Sommarskog 134.7K Reputation points MVP Volunteer Moderator
2023-11-25T17:29:34.7133333+00:00

From what you say, I don't think partitioning is a good solution to the problem.

It is true that if the date column you query by today is not indexed, there would be less to scan if you partition the table, since SQL Server only has to scan the partitions that contains the interval. On the other hand, queries that do not include the partitioning column will be less efficient since they have to visit every partition. You say that rows are updated; I assume that updates are by the primary key, alert_id. These updates will be a lot less efficient, unless they are rewritten to also include the partitioning column.

I think a better option is to simply add a traditional index on the column(s) you query by.

By the way, I note that the columns start_time, end_time and date are all nvarchar. This is a very bad choice for date/time data. It will not take long before you have a record for Nov 31st. Also, in which format do you store the dates? If the format is not YYYYMMDD, there is no indexing - or partitioning - to save you. I strongly recommend that you change these columns to use proper data types. That is, datetime2 with the proper scale for start_time and end_time and date for the date column.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.