What is the function of this query statement of sql - 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.

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?

Why is my filtered index not being used for one statement?

I have the following table (an excerpt follows w/o other columns):
USE [opg-systems-dev]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Orders]
(
[SyncChannelEngineOrder] [bit] NOT NULL,
[IsSyncing] [bit] NOT NULL,
CONSTRAINT [PK_Orders]
PRIMARY KEY CLUSTERED ([Id] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Orders]
ADD DEFAULT (CONVERT([bit], (0))) FOR [IsSyncing]
GO
-- -----
CREATE NONCLUSTERED INDEX [IX_Orders_SyncChannelEngineOrder]
ON [dbo].[Orders] ([SyncChannelEngineOrder] ASC)
WHERE ([SyncChannelEngineOrder] <> (0))
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON,
OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
CREATE NONCLUSTERED INDEX [IX_Orders_IsSyncing]
ON [dbo].[Orders] ([IsSyncing] ASC)
WHERE ([IsSyncing] <> (0))
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON,
OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
I'm trying to use a filtered index here - but it seems when I'm querying that the the index for SyncChannelEngineOrder is not being used for some reason.
The setup is supposed to be the same for both columns.
The query is requesting all columns with a select *. The indexes only contain one column (ignoring the clustered key). To provide the missing column data that is not on the index, the query optimizer is calculating a lower cost to just scan all of the rows in the source table.
If SQL Server chose to use the existing indexes, it would first scan those rows, and then it would perform a bookmark lookup to get the rest of the columns from the table. If there are very few rows in the filtered index, and the table is very large, the SQL Server query optimizer could theoretically calculate a low enough cost to use the filtered index to limit the number of rows. Otherwise, bookmark lookups are expensive on a large number of rows. I am guessing the number of rows in the index is over this threshold.
SQL Server will add the clustered key to a non-clustered index to facilitate the bookmark lookup. If the query were to request only the clustered key and the index filter columns, the indexes would cover the query, and I expect the query optimizer would choose to use the filtered indexes.

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.

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

SQL stored procedure drop and create indexes

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!