SQL stored procedure drop and create indexes - sql

I need to truncate a table to refresh data, however this table has an indexed view depends on it. I'm trying to drop indexes, then truncate table, then recreate indexes. I get an error that the non-clustered indexes don't exist or I don't have permission to drop them... code lives inside a stored procedure shown below. I am assuming it has something to do with the order of execution in the stored procedure as executing the code (drop/truncate/create) manually works.
DROP INDEX [IDX_VDetailEmergency] ON [dbo].[vFactEmergencySummary] WITH ( ONLINE = OFF )
DROP INDEX [IDX_VDetailEmergency_facility_refno] ON [dbo].[vFactEmergencySummary] WITH ( ONLINE = OFF )
DROP INDEX [IDX_VDetailEmergency_mode_of_separation_refno] ON [dbo].[vFactEmergencySummary] WITH ( ONLINE = OFF )
DROP INDEX [IDX_VDetailEmergency_period_refno] ON [dbo].[vFactEmergencySummary] WITH ( ONLINE = OFF )
truncate table DetailEmergency
CREATE UNIQUE CLUSTERED INDEX [IDX_VDetailEmergency] ON [dbo].[vFactEmergencySummary]
(
[facility_refno] ASC,
[period_refno] ASC,
[mode_of_separation_refno] ASC,
[ed_visit_type_refno] ASC,
[triage_category] ASC,
[UDG_refno] ASC,
[URG_refno] ASC,
[URG_MDB_refno] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
CREATE NONCLUSTERED INDEX [IDX_VDetailEmergency_facility_refno] ON [dbo].[vFactEmergencySummary]
(
[facility_refno] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
CREATE NONCLUSTERED INDEX [IDX_VDetailEmergency_mode_of_separation_refno] ON [dbo].[vFactEmergencySummary]
(
mode_of_separation_refno ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
CREATE NONCLUSTERED INDEX [IDX_VDetailEmergency_period_refno] ON [dbo].[vFactEmergencySummary]
(
period_refno ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

You may follow these steps with SQL Server Management Studio if you are okay with dropping and recreating the table :
Right click on the database in which your table is available
Click Tasks -> Generate scripts
Press next to navigate to Choose Objects
Select specific database objects
Select your table
Press Advanced
Find Script DROP and CREATE in the list and change the drop down list as Script DROP and CREATE
Scroll down if you want to include Indexes or Constraints etc. in your script
Press OK and choose Save to new query window to see your script on a new query window
Press Next and Finish
Happy administrating!

Related

Issue with re-create index/constraint after dropping it

I am using SQL Server 2008 R2.
I encountered this issue when re-create index.
As I need to alter column, so I drop constraint/index first and create back my constraint/index.
However, it shows error message saying
The operation failed because an index or statistics with name 'ABC' already exists on table 'test_table'
I wonder why would this error message shown? Since I have drop constraint
I wrote this to drop index
DROP INDEX [ABC] ON [dbo].[test_table] WITH ( ONLINE = OFF )
I then re-create index
CREATE NONCLUSTERED INDEX [ABC] ON [dbo].[test_table]
(
[col_1] ASC,
[col_2] ASC,
[col_3] ASC
)
INCLUDE ( [col_4],
[col_5]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
Anyone has any idea what's wrong here?

Partition of existing table with huge data in SQL

I have a table which contains 2 cr records in table. I am trying to do its partition based on Month and Year.
I tried it with creating a filegroups of this tables but in my scenario that table is using on many places in pre coding part. Is there any way so I can partition this table and use it in BI reports so pre programming doesn't impact.
For edition : -
I have the following pretty basic query but it takes 10 mins to run.
Here is the execution plan - https://www.brentozar.com/pastetheplan/?id=B1dy0ZQ6d
Can anyone see a way of improving it? Let me know if some sample data/table structures would be useful.
E2E_TBL_LIQUIDITY_TRACKING_CFY_JUNE has 899556 records LQTFYOpeningStock has 934878 records E2E_TBL_CPL_SALES_MR_008 has 131491 records E2E_TBL_MATERIAL_MASTER has 4695 records LocationNameView has 477 records E2e_Tbl_Customer_Master has 20390 records E2e_Tbl_Lob_Master has 5 records
Below are indexes : -
CREATE NONCLUSTERED INDEX [Index1LQt] ON [dbo].[E2E_TBL_LIQUIDITY_TRACKING_CFY_JUNE]
(
[Territory_Code] ASC
)
INCLUDE ( [Customer_Code],
[Product_Code],[LOB_Code],[Distributor_Stock],[Dealers_Stock],[L3_Price],[L1_Price],[L2_Price])
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [Index2LQt] ON [dbo].[LQTFYOpeningStock]
(
[Customer_Code] ASC,
[Product_Code] ASC,
[Territory_Code] ASC,
[LOB_Code] ASC
)
INCLUDE ( [StockValueL1],
[StockValueL2],
[StockValueL3]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [Index3LQt] ON [dbo].[LQTFYOpeningStock]
(
[Territory_Code] ASC
)
INCLUDE ( [Customer_Code],
[Product_Code],
[LOB_Code],
[StockValueL1],
[StockValueL2],
[StockValueL3]) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[E2E_TBL_CPL_SALES_MR_008] ADD CONSTRAINT [PK_MR008] PRIMARY KEY CLUSTERED
(
[Territory_Code] ASC,
[Customer_Code] ASC,
[Product] ASC,
[SKU] ASC,
[LOB] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
You can partition without having multiple filegroups. Ideally (for performance reasons) each partition should be on a different filegroup on a different drive. But partitioning will work just fine (typically slower if the partitions are large_ on a single filegroup. Its easy to later move partitions between filegroups.

What is the function of this query statement of sql

USE [DataStore]
GO
SET ANSI_PADDING ON
GO
CREATE NONCLUSTERED INDEX [idx_alf_cass_qnln] ON [dbo].[alf_child_assoc]
(
[parent_node_id] ASC,
[qname_ns_id] ASC,
[qname_localname] ASC,
[qname_crc] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
It creates an nonclustered index.
Generally, nonclustered indexes are created to improve the performance of frequently used queries not covered by the clustered index or to locate rows in a table without a clustered index (called a heap). You can create multiple nonclustered indexes on a table or indexed view.

Non Clustered Index on UniqueIdentifier columns

WE have a simple SQL query selecting two columns like below
SELECT TEAMID,iSvALID FROM teams where teamid='{{guid}}' and subid = {{guid}}.
we had issues with performance of this query as there was no index which I also have added now... like
CREATE NONCLUSTERED INDEX [NonClusteredIndex-20170725-191322] ON [dbo].[TableName]
(
[subid ] ASC,
[TeamId] ASC,
[Date] ASC
)
INCLUDE ([IsVAlid])
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
The problem now is that it takes a few Milliseconds sometimes to execute the query but a few seconds!!! some times .
And because of some constraints we can't change the type of GUID column and also we get this from an exernal system.
Is there a way we can still make sure performance will be good

Index creation job failing because of quotation

I have a job that is part of my staging process and it includes indexing a table post population.
One of the indexes is a filtered index:
CREATE NONCLUSTERED INDEX [IDX_IP_ActivePAss] ON [dbo].[IPStg]
(
[SIP] ASC,
[EIP] ASC
)
WHERE ([Status] IN ("Active", "Private"))
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF,
DROP_EXISTING = OFF,
ONLINE = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]
GO
The job fails with the following error:
CREATE INDEX failed because the following SET options have incorrect
settings: 'QUOTED_IDENTIFIER'. Verify that SET options are correct
for use with indexed views and/or indexes on computed columns and/or
filtered indexes and/or query notifications and/or XML data type
methods and/or spatial index operations. [SQLSTATE 42000] (Error
1934). The step failed.
Please advise.
I would expect to see single quotes not double. I think its a typo.
CREATE NONCLUSTERED INDEX [IDX_IP_ActivePAss] ON [dbo].[IPStg] (
[SIP] ASC,
[EIP] ASC
) WHERE ( [Status] IN ('Active', 'Private' )
) WITH ( PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF,
DROP_EXISTING = OFF,
ONLINE = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY] GO