I've got a simple table and I'm evaluating which is the best way to delete some records from it.
The DELETE query is so simple: DELETE FROM [dbo].[Badge] WHERE [Id_Operatore] = #SOMEVALUE
Is there a way to increase performance of this operation?
This is the table:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Badge](
[Id_Badge] [int] IDENTITY(1,1) NOT NULL,
[Id_Operatore] [int] NULL,
[Badge0] [smallint] NULL,
[Badge1] [smallint] NULL,
[Badge2] [smallint] NULL,
[Badge3] [smallint] NULL,
[Badge4] [smallint] NULL,
[Badge5] [smallint] NULL,
[Badge6] [smallint] NULL,
[Badge7] [smallint] NULL,
[Badge8] [smallint] NULL,
[Badge9] [smallint] NULL,
[Badge10] [smallint] NULL,
[Badge11] [smallint] NULL,
[Badge12] [smallint] NULL,
[Badge13] [smallint] NULL,
[Badge14] [smallint] NULL,
[Badge15] [smallint] NULL,
[Badge16] [smallint] NULL,
[Badge17] [smallint] NULL,
[Badge18] [smallint] NULL,
[Badge19] [smallint] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Badge] WITH CHECK ADD CONSTRAINT [Badge_FK00] FOREIGN KEY([Id_Operatore])
REFERENCES [dbo].[Operatori] ([ID_Operatore])
ON UPDATE CASCADE
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[Badge] CHECK CONSTRAINT [Badge_FK00]
GO
You should add an index on the foreign key.
The index might be covering, so including all these badges is not a bad idea.
Create a clustered (preferred) or nonclustered index on [Id_Operatore].
"Clustered" keeps the data physical by each other.
This reduces the IO on delete.
Related
I have created a partition table and trying to query the partition but not able to do so in sql server 2016. Could somebody tell me where I am going wrong
CREATE PARTITION FUNCTION [financialStatementPartition](datetime)
AS RANGE RIGHT FOR VALUES (N'2013-01-01T00:00:00.000', N'2014-01-01T00:00:00.000',
N'2015-01-01T00:00:00.000', N'2016-01-01T00:00:00.000',
N'2017-01-01T00:00:00.000')
GO
Table schema
CREATE TABLE [dbo].[FinancialStatementIds]
(
[financialCollectionId] [int] NOT NULL,
[companyId] [int] NOT NULL,
[dataItemId] [int] NOT NULL,
[dataItemName] [varchar](200) NULL,
[dataItemvalue] [decimal](18, 0) NULL,
[unittypevalue] [int] NULL,
[fiscalyear] [int] NULL,
[fiscalquarter] [int] NULL,
[periodenddate] [datetime] NULL,
[filingdate] [datetime] NULL,
[restatementtypename] [varchar](200) NULL,
[latestforfinancialperiodflag] [bit] NULL,
[latestfilingforinstanceflag] [bit] NULL,
[currencyconversionflag] [int] NULL,
[currencyname] [varchar](200) NULL,
[periodtypename] [varchar](200) NULL
)
Query
SELECT *
FROM dbo.FinancialStatementIds
WHERE $PARTITION.financialStatementPartition(periodenddate) = '2013-01-01T00:00:00.000'
Error
Conversion failed when converting the varchar value '2013-01-01T00:00:00.000' to data type int.
Use following query instead:
SELECT *
FROM dbo.FinancialStatementIds
WHERE periodenddate = '20130101'
Anyway, the table is still not partitioned properly. You need to apply Partition Scheme to connect the table and Partition function.
I have 2 tables
tbl_jobs
CREATE TABLE [dbo].[tbl_jobs]
(
[JobID] [int] IDENTITY(1,1) NOT NULL,
[JobType] [nvarchar](50) NOT NULL,
[RequestID] [int] NOT NULL,
[AssignTo] [int] NOT NULL,
[FromOrgID] [int] NOT NULL,
[ToOrgID] [int] NOT NULL,
[Ammount] [nvarchar](50) NOT NULL,
[JobStatus] [nvarchar](50) NOT NULL,
[Remark] [nvarchar](50) NULL,
[strOwner] [nvarchar](50) NOT NULL,
[dbTstamp] [datetime2](7) NOT NULL,
CONSTRAINT [PK_tbl_jobs]
PRIMARY KEY CLUSTERED ([JobID] ASC)
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tbl_jobs] WITH CHECK
ADD CONSTRAINT [FK_tbl_jobs_tbl_orgs]
FOREIGN KEY([FromOrgID]) REFERENCES [dbo].[tbl_orgs] ([OrgID])
GO
ALTER TABLE [dbo].[tbl_jobs] CHECK CONSTRAINT [FK_tbl_jobs_tbl_orgs]
GO
ALTER TABLE [dbo].[tbl_jobs] WITH CHECK
ADD CONSTRAINT [FK_tbl_jobs_tbl_orgs1]
FOREIGN KEY([ToOrgID]) REFERENCES [dbo].[tbl_orgs] ([OrgID])
GO
tbl_orgs
CREATE TABLE [dbo].[tbl_orgs]
(
[OrgID] [int] IDENTITY(1,1) NOT NULL,
[OrgName] [nvarchar](50) NOT NULL,
[OrgTele] [nvarchar](50) NULL,
[OrgEmail] [nvarchar](50) NULL,
[OrgArea] [nvarchar](50) NOT NULL,
[OrgCity] [nvarchar](50) NOT NULL,
[OrgLocation] [nvarchar](50) NOT NULL,
[OrgType] [nvarchar](50) NOT NULL,
[OrgStatus] [nvarchar](50) NOT NULL,
[strOwner] [nvarchar](50) NOT NULL,
[dbTStamp] [datetime2](7) NOT NULL,
CONSTRAINT [PK_tbl_orgs]
PRIMARY KEY CLUSTERED ([OrgID] ASC)
) ON [PRIMARY]
GO
I need to get most of the tbl_jobs columns and corresponding tbl_orgs.OrgName for tbl_jobs.FromOrgID & tbl_jobs.ToOrgID.
If I choose tbl_orgs.orgname, I don't get the proper results.
I am stuck here. What type of query should I use to get the result.?
You're joining two times to the same table instance. You should click on "add table" and add tbl_orgs one more time and join "FromOrgID" to one instance of it and "ToOrgID" to the other one. Otherwise the join doesn't make sense unless "FromOrgID" and "ToOrgID" are equal.
What does the output of "Script Table As -> CREATE To" mean ? I have a partitioned table, but when I right click on it in the Management Studio and choose "Script Table As -> CREATE To", the output didn't contain anything related to table partition. I was expected to find the "ON partition_scheme_name", but it didn't show up.
USE [FcstDB]
GO
/****** Object: Table [dbo].[StFcst] Script Date: 2014-06-10 15:54:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[StFcst](
[MID] [tinyint] NOT NULL,
[INITDATE] [smalldatetime] NOT NULL,
[FH] [tinyint] NOT NULL,
[STID] [char](5) NOT NULL,
[WW03] [smallint] NOT NULL,
[WW06] [smallint] NOT NULL,
[WW12] [smallint] NOT NULL,
[T] [smallint] NOT NULL,
[TMAX12] [smallint] NOT NULL,
[TMIN12] [smallint] NOT NULL,
[PR03] [smallint] NOT NULL,
[PR06] [smallint] NOT NULL,
[PR12] [smallint] NOT NULL,
[PR24] [smallint] NOT NULL,
[RH] [smallint] NOT NULL,
[WD] [smallint] NOT NULL,
[WS] [smallint] NOT NULL,
[CLOUD] [smallint] NOT NULL,
[VV] [int] NOT NULL,
[SLP] [smallint] NOT NULL,
[E_T] [smallint] NOT NULL,
[E_TMAX12] [smallint] NOT NULL,
[E_TMIN12] [smallint] NOT NULL,
[E_PR03] [smallint] NOT NULL,
[E_PR06] [smallint] NOT NULL,
[E_PR12] [smallint] NOT NULL,
[E_PR24] [smallint] NOT NULL,
[E_WD] [smallint] NOT NULL,
[E_WS] [smallint] NOT NULL,
CONSTRAINT [PK_StFcst] PRIMARY KEY CLUSTERED
(
[INITDATE] DESC,
[FH] ASC,
[MID] ASC,
[STID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
GO
Script Table As -> CREATE To creates script containing Create command of given table structure without any kind of data.
Do you intend to copy the data somewhere else? If yes, consider using SELECT INTO command.
For Create Partition scheme please visit msdn.
Script table documentation here.
This is maddening! Code in question has been running for over 5 years.
Here's the scoop....
I am doing an INSERT...SELECT into a table with a primary key that is an identity column. I do not specify the key when I insert - SQL Server generates it as expected.
I am doing the insert in a stored procedure that I call in a loop (for loop in SSIS, actually). The stored procedure will insert rows in batches (configurable). It might insert 1000 rows at a time or it might insert 50,000 - doesn't matter. It will work for a random number of calls (inserting thousands of rows) and then it will fail, out of the blue, with a
Violation of primary key / duplicate
error. If I check the identity seed - it is correct. If I kick off the process again it will work fine, for a while.
The values being inserted are coming from 2 tables that I join together, as if that matters.
The bulk of my code is below:
WHILE #pk <= #max_pk
BEGIN
INSERT INTO tbl_claim_line (fk_batch_control_group, fk_claim, fk_provider, service_from_date, service_to_date, allowed, net_paid, COB, flex_1, flex_2, flex_3, flex_4)
SELECT
#fk_batch_control_group
, c.pk_claim
, p.pk_provider
, i.date_of_service_from
, i.date_of_service_to
, i.allowed_amount
, i.net_paid_amount
, i.cob_amount
, i.claimline_flex_1
, i.claimline_flex_2
, i.claimline_flex_3
, i.claimline_flex_4
FROM
tbl_import i
INNER JOIN
tbl_import__claim c ON i.claim_number = c.claim_number
LEFT JOIN
tbl_import__provider p ON ISNULL(i.provider_type,'') = ISNULL(p.provider_type,'')
AND ISNULL(i.provider_specialty,'') = ISNULL(p.provider_specialty,'')
AND ISNULL(i.provider_zip_code,'') = ISNULL(p.provider_zip_code,'')
WHERE
pk_import = #pk
UPDATE tbl_import
SET fk_claim_line = SCOPE_IDENTITY()
WHERE pk_import = #pk
SET #pk += 1
END
--TABLE DEFINITIONS...
CREATE TABLE [dbo].[tbl_claim_line](
[fk_batch_control_group] [int] NOT NULL,
[fk_claim] [int] NOT NULL,
[fk_provider] [int] NULL,
[service_from_date] [date] NULL,
[service_to_date] [date] NULL,
[allowed] [money] NULL,
[net_paid] [money] NULL,
[COB] [money] NULL,
[flex_1] [varchar](200) NULL,
[flex_2] [varchar](200) NULL,
[flex_3] [varchar](200) NULL,
[flex_4] [varchar](200) NULL,
[pk_claim_line] [int] IDENTITY(1,1) NOT NULL,
[insert_date] [datetime] NOT NULL,
CONSTRAINT [PK_tbl_claim_line] PRIMARY KEY NONCLUSTERED
(
[pk_claim_line] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[tbl_claim_line] WITH CHECK
ADD CONSTRAINT [FK_tbl_claim_line_tbl_batch_control_group]
FOREIGN KEY([fk_batch_control_group])
REFERENCES [dbo].[tbl_batch_control_group] ([pk_batch_control_group])
GO
ALTER TABLE [dbo].[tbl_claim_line] CHECK CONSTRAINT [FK_tbl_claim_line_tbl_batch_control_group]
GO
ALTER TABLE [dbo].[tbl_claim_line] WITH CHECK
ADD CONSTRAINT [FK_tbl_claim_line_tbl_claim]
FOREIGN KEY([fk_claim])
REFERENCES [dbo].[tbl_claim] ([pk_claim])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[tbl_claim_line] CHECK CONSTRAINT [FK_tbl_claim_line_tbl_claim]
GO
ALTER TABLE [dbo].[tbl_claim_line] WITH CHECK
ADD CONSTRAINT [FK_tbl_claim_line_tbl_provider]
FOREIGN KEY([fk_provider])
REFERENCES [dbo].[tbl_provider] ([pk_provider])
GO
ALTER TABLE [dbo].[tbl_claim_line] CHECK CONSTRAINT [FK_tbl_claim_line_tbl_provider]
GO
ALTER TABLE [dbo].[tbl_claim_line] ADD CONSTRAINT [DF_tbl_claim_line__insert_date] DEFAULT (getdate()) FOR [insert_date]
GO
----second table
CREATE TABLE [dbo].[tbl_import](
[fk_claim_line] [int] NULL,
[member_id] [varchar](50) NULL,
[member_card_id] [varchar](50) NULL,
[member_first_name] [varchar](50) NULL,
[member_last_name] [varchar](50) NULL,
[member_dob] [varchar](50) NULL,
[member_gender] [varchar](50) NULL,
[member_subscriber_relationship_code] [varchar](50) NULL,
[member_address_line_1] [varchar](100) NULL,
[member_address_line_2] [varchar](100) NULL,
[member_city] [varchar](50) NULL,
[member_state] [varchar](50) NULL,
[member_zip] [varchar](50) NULL,
[member_phone] [varchar](50) NULL,
[member_email] [varchar](50) NULL,
[subscriber_id] [varchar](50) NULL,
[group_line_of_business] [varchar](50) NULL,
[group_product] [varchar](50) NULL,
[group_employer] [varchar](50) NULL,
[provider_first_name] [varchar](50) NULL,
[provider_last_or_full_name] [varchar](200) NULL,
[provider_type] [varchar](200) NULL,
[provider_specialty] [varchar](400) NULL,
[provider_zip_code] [varchar](50) NULL,
[provider_tax_id] [varchar](50) NULL,
[medical_code_1] [varchar](10) NULL,
[medical_code_1_description] [varchar](500) NULL,
[medical_code_2] [varchar](10) NULL,
[medical_code_2_description] [varchar](500) NULL,
[medical_code_3] [varchar](10) NULL,
[medical_code_3_description] [varchar](500) NULL,
[medical_code_4] [varchar](10) NULL,
[medical_code_4_description] [varchar](500) NULL,
[medical_code_5] [varchar](10) NULL,
[medical_code_5_description] [varchar](500) NULL,
[medical_code_6] [varchar](10) NULL,
[medical_code_6_description] [varchar](500) NULL,
[medical_code_7] [varchar](10) NULL,
[medical_code_7_description] [varchar](500) NULL,
[medical_code_8] [varchar](10) NULL,
[medical_code_8_description] [varchar](500) NULL,
[medical_code_9] [varchar](10) NULL,
[medical_code_9_description] [varchar](500) NULL,
[medical_code_10] [varchar](10) NULL,
[medical_code_10_description] [varchar](500) NULL,
[medical_code_11] [varchar](10) NULL,
[medical_code_11_description] [varchar](500) NULL,
[medical_code_12] [varchar](10) NULL,
[medical_code_12_description] [varchar](500) NULL,
[medical_code_13] [varchar](10) NULL,
[medical_code_13_description] [varchar](500) NULL,
[medical_code_14] [varchar](10) NULL,
[medical_code_14_description] [varchar](500) NULL,
[medical_code_15] [varchar](10) NULL,
[medical_code_15_description] [varchar](500) NULL,
[medical_code_16] [varchar](10) NULL,
[medical_code_16_description] [varchar](500) NULL,
[date_of_service_from] [varchar](50) NULL,
[date_of_service_to] [varchar](50) NULL,
[claim_number] [varchar](50) NULL,
[claim_line_number] [varchar](50) NULL,
[original_claim_number] [varchar](50) NULL,
[allowed_amount] [varchar](50) NULL,
[net_paid_amount] [varchar](50) NULL,
[cob_amount] [varchar](50) NULL,
[date_paid] [varchar](50) NULL,
[member_flex_1] [varchar](200) NULL,
[member_flex_2] [varchar](200) NULL,
[member_flex_3] [varchar](200) NULL,
[member_flex_4] [varchar](200) NULL,
[claim_flex_1] [varchar](200) NULL,
[claim_flex_2] [varchar](200) NULL,
[claim_flex_3] [varchar](200) NULL,
[claim_flex_4] [varchar](200) NULL,
[claimline_flex_1] [varchar](200) NULL,
[claimline_flex_2] [varchar](200) NULL,
[claimline_flex_3] [varchar](200) NULL,
[claimline_flex_4] [varchar](200) NULL,
[pk_import] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK_tbl_import] PRIMARY KEY NONCLUSTERED
(
[pk_import] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
I ran into this and much like user3170349, it was a seed issue on the column. I'm adding some additional info, however.
First, you can run this to figure out if you have a seed problem:
DBCC CHECKIDENT ('TABLE_NAME_GOES_HERE', NORESEED);
This will give you information which will read something like this:
Checking identity information: current identity value 'XXXX', current column value 'YYYY'.
If YYYY is larger than XXXX, then you have a problem and need to RESEED the table to get things going again. You can do so with the following command:
DBCC CHECKIDENT ('TABLE_NAME_GOES_HERE', RESEED, ZZZZZ);
Where ZZZZ is the reseed value. That value should be at least one higher than YYYY. YMMV, so pick a value that is appropriate for your situation.
"Code in question has been running for over 5 years."
"It might insert 1000 records at a time or it might insert 50,000 "
Is it possible you have finally overflowed the integer type of the primary key?
Did it wrap around and is now starting over? That would cause duplicate primary keys.
I'm looking to store the contents of several dropdownlists in my SQL Server. Is it better to store them in 1 table per dropdown, or in a larger table?
My larger table would have schema like:
CREATE TABLE [dbo].[OptionTable](
[OptionID] [int] IDENTITY(1,1) NOT NULL,
[ListName] [varchar](100) NOT NULL,
[DisplayValue] [varchar](100) NOT NULL,
[Value] [varchar](100) NULL,
[OptionOrder] [tinyint] NULL,
[AssociatedDept] [int] NULL,
[Other2] [nchar](10) NULL,
[Other3] [nchar](10) NULL
) ON [PRIMARY]
And I would get the contents of 1 list by doing something like:
Select [columns]
From OptionTable
WHERE ListName = 'nameOfList'
So how can I decide? I know it will work like this, I'm just not sure if this is good practice or not? Will one way perform better? What about readability? Opinions appreciated.
I've worked in databases that had a single "super option table" that contained values for multiple drop down lists... it worked OK for the drop down list population, but when I needed to use those values for other reporting purposes, it became a pain because the "super option table" needed to be filtered based on the specific set of options that I needed, and it ended up in some ugly looking queries.
Additionally, down the road there were conditions that required an additional value to be tracked with one of the lists... but that column would need to be added to the whole table, and then all the other sets of options within that table would simply have a NULL for a column that they didn't care about...
Because of that, I'd suggest if you're dealing with completely distinct lists of data, that those lists be stored in separate tables.
The quick and easy:
CREATE TABLE [dbo].[Lists](
[ListId] [int] IDENTITY(1,1) NOT NULL,
[ListName] [varchar](100) NOT NULL,
--these could be associated with lists or options, wasn't specified
[AssociatedDept] [int] NULL,
[Other2] [nchar](10) NULL,
[Other3] [nchar](10) NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[Options](
[OptionId] [int] IDENTITY(1,1) NOT NULL,
[ListId] [int] NOT NULL,
[DisplayValue] [varchar](100) NOT NULL,
[Value] [varchar](100) NULL,
[OptionOrder] [tinyint] NULL,
--these could be associated with lists or options, wasn't specified
[AssociatedDept] [int] NULL,
[Other2] [nchar](10) NULL,
[Other3] [nchar](10) NULL
) ON [PRIMARY]
Get contents with
select Options.* --or a subset
from Options as o
join Lists as l
on l.ListId=o.ListId and l.ListName = 'nameOfList'
order by o.OptionOrder
The (potentially: depends on your data) more optimized (particularly if one option appears in more than one list)
CREATE TABLE [dbo].[Lists](
[ListId] [int] IDENTITY(1,1) NOT NULL,
[ListName] [varchar](100) NOT NULL,
--these could be associated with lists or options, wasn't specified
[AssociatedDept] [int] NULL,
[Other2] [nchar](10) NULL,
[Other3] [nchar](10) NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[Options](
[OptionId] [int] IDENTITY(1,1) NOT NULL,
[DisplayValue] [varchar](100) NOT NULL,
[Value] [varchar](100) NULL,
--these could be associated with lists or options, wasn't specified
[AssociatedDept] [int] NULL,
[Other2] [nchar](10) NULL,
[Other3] [nchar](10) NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[ListOptions](
[OptionId] [int] NOT NULL,
[ListId] [int] NOT NULL,
[OptionOrder] [tinyint] NULL,
--these could be associated with lists or options, wasn't specified
[AssociatedDept] [int] NULL,
[Other2] [nchar](10) NULL,
[Other3] [nchar](10) NULL
)
Get contents with
select Options.* --or a subset
from Options as o
join ListOptions as lo
on lo.OptionId=o.OptionId
join Lists as l
on l.ListId=lo.ListId and l.ListName = 'nameOfList'
order by lo.OptionOrder
On either, you'd want to index the foreign key columns.