Date time index sql server 2005 - sql

I know this is a common topic but i stil feel my scenario requires some custom advice. I was selecting from a table the other day and it took me an AGE to select on a datetime column that has not been indexed. I want to index this, only problem ebing is that the "production" 2005 box i am unfamiliar with and how it will handle the index creation. With that in mind i am wondering what the safest way for me to create an index on the table is. All the details i think people will need are below (i hope) :
Index to be created on field 5 (possibly field4 also)
Table 1 definition:
CREATE TABLE [dbo].[TABLE 1](
[ID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_BLA] DEFAULT (newid()),
[field1] [nvarchar](7) NULL,
[field2] [nvarchar](10) NULL,
[field3] [nvarchar](2) NULL,
[field4] [datetime] NULL,
[field5] [datetime] NULL,
[field6] [smallint] NULL,
[field7] [nvarchar](1) NULL,
[field8] [nvarchar](7) NULL,
[field9] [nvarchar](60) NULL,
[field10] [smallint] NULL,
[field11] [nvarchar](15) NULL,
[field12] [datetime] NULL,
CONSTRAINT [PK_ID] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Total count of rows in Table 1 : 2926836

Related

Composite key with one component being a foreign key

I have a financial app where I store ticker data on a daily basis, so i create a primary key from the Ticker and Date.
I also need to make Ticker (which is part of the above composite key) a foreign key of another table called Tickers.
However when I go to add the Foreign Key relationship within SQL Server Management Studio, it is asking me to provide link for both columns Ticker and Date, whereas the Ticker table only contains Ticker (which is what I only want to bind on).
Any ideas on what I am doing wrong please or is this not possible?
My tables are as below:
CREATE TABLE [dbo].[Tickers](
[Ticker] [nvarchar](8) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[Market] [int] NOT NULL,
[Locale] [int] NOT NULL,
[Type] [int] NOT NULL,
[Active] [bit] NOT NULL,
[PrimaryExch] [nvarchar](50) NOT NULL,
[Updated] [datetime2](7) NOT NULL,
[Currency] [int] NOT NULL,
CONSTRAINT [PK_Tickers] PRIMARY KEY CLUSTERED
(
[Ticker] 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
CREATE TABLE [dbo].[AggregateDay](
[Ticker] [nchar](8) NOT NULL,
[Date] [date] NOT NULL,
[Volume] [decimal](18, 0) NULL,
[Open] [decimal](18, 0) NULL,
[Close] [decimal](18, 0) NULL,
[High] [decimal](18, 0) NULL,
[Low] [decimal](18, 0) NULL,
[Samples] [int] NULL,
CONSTRAINT [PK_AggregateDay] PRIMARY KEY CLUSTERED
(
[Ticker] ASC,
[Date] 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
One problem with your set-up is that the parent and child columns do not have the same datatype: [dbo].[Tickers]([Ticker]) is [nvarchar](8), while [dbo].[AggregateDay](Ticket) is [nchar](8).
If you align both datatypes, then you can create the relationship, like so:
CREATE TABLE [dbo].[AggregateDay](
[Ticker] [nvarchar](8) NOT NULL
REFERENCES [dbo].[Tickers]([Ticker]), -- foreign key declaration
[Date] [date] NOT NULL,
[Volume] [decimal](18, 0) NULL,
[Open] [decimal](18, 0) NULL,
[Close] [decimal](18, 0) NULL,
[High] [decimal](18, 0) NULL,
[Low] [decimal](18, 0) NULL,
[Samples] [int] NULL,
CONSTRAINT [PK_AggregateDay] PRIMARY KEY CLUSTERED
([Ticker] ASC, [Date] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]
) ON [PRIMARY]
Demo on DB Fiddle

performance issue in retrieving data from a varbinary(MAX) field

how i can improve the performance of the sq statement which contains Varbinary(Max) datatype
Table Structure
CREATE TABLE [dbo].[Table_1](
[EmpID] [numeric](18, 0) NOT NULL,
[SrNo] [numeric](18, 0) NOT NULL,
[Type] [varchar](10) NULL,
[FileName] [varchar](100) NULL,
[D1] [varchar](50) NULL,
[D2] [varchar](50) NULL,
[Data] [varbinary](max) NULL,
[CreatedBy] [varchar](50) NULL,
[CreatedOn] [datetime] NULL,
CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED
(
[EmpID] ASC,
[SrNo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
the Select Query takes around 2 to 3 seconds to select a single record with
Data
Checked Indexing of the Table

How to check an id being tied to its related table for only one value

I have two tables in which I need to check whether UCCID is tied to only one BUYID as part of testing. UCCID should be unique for my table. But BUyid can have multiple UCCID. But UCCID should be tied to unique (only one) BUYID.
CREATE TABLE [dbo].[EquipmentBase](
[EDAEquipmentId] [uniqueidentifier] NOT NULL,
[BuyID] [nvarchar](100) NULL,
[EqtAE] [nvarchar](100) NULL,
[EqtSn] [nvarchar](100) NULL,
[EqtSz] [nvarchar](100) NULL,
[EqtUccYr] [nvarchar](100) NULL,
[EqtUnit] [nvarchar](100) NULL,
[EqtValue] [nvarchar](100) NULL,
[UccID] [NVARCHAR](100) NULL,
CONSTRAINT [PK__EquipmentBase] PRIMARY KEY CLUSTERED
(
[_EDAEquipmentId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[activityBase](
[activityId] [UNIQUEIDENTIFIER] NOT NULL,
[UccID] [NVARCHAR](100) NULL,
[ASCity] [NVARCHAR](100) NULL,
[ASClass] [NVARCHAR](100) NULL,
[ASComp] [NVARCHAR](100) NULL,
[ASID] [NVARCHAR](100) NULL,
[ASState] [NVARCHAR](100) NULL,
[BuyADR1] [NVARCHAR](100) NULL,
[BuyADR2] [NVARCHAR](100) NULL,
[BuyID] [NVARCHAR](100) NULL,
[UccCom] [NVARCHAR](100) NULL,
[UccDate] [DATETIME] NULL,
[UccFips2] [NVARCHAR](100) NULL,
[UccStatus] [NVARCHAR](100) NULL,
[UccTransID] [NVARCHAR](100) NULL,
CONSTRAINT [PK__activityBase] PRIMARY KEY CLUSTERED
(
[activityId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]
GO
I tried the below query but I am not getting the result for what I am looking into? Can anyone help pls? I expect the below query to return 0 so that UCCID is tied to single BUYID.
SELECT COUNT(*) FROM dbo.activityBase ab
JOIN (SELECT eb.BuyID FROM dbo.EquipmentBase eb
WHERE eb.UccID IS NOT NULL
GROUP BY eb.BuyID
HAVING COUNT(eb.BuyID)=1)c
ON c.BuyID = ab.BuyID
If I'm understanding correctly (and your question is not super clear), if you want to identify any values of UccID for which records exist with more than one different value of BuyID(across both of your tables), you could use:
SELECT UccId, COUNT(*)
FROM
(
SELECT DISTINCT BuyID, UccID FROM EquipmentBase
UNION
SELECT DISTINCT BuyID, UccID FROM activityBase
) AS DistinctCombos
GROUP BY UccId HAVING COUNT(*) > 1
This uses a subquery in the FROM clause to return all combinations of the two fields across both tables, then inspects those results to find any UccID values which have more than one distinct BuyID value against them.

Conversion failed when converting date and/or time from character string. What could be wrong?

INSERT INTO dbo.SaleNew(ENQ_AID,DOCKET_NO,SALE_TYPE,VEHICLE_MODEL,SALE_DATE,BOOKING_DATE,DELIVERY_DATE,
DEALER_NAME,ENQ_GEN_BY,EXEC_NAME,USER_CR,DATE_CR) SELECT '6','2','0','TEST','2016-05-01','2016-05-10','2016-05-15',
'ABC','S I','V B','1',SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30')
What could be wrong with this query?
Also tried:
INSERT INTO dbo.SaleNew(ENQ_AID,DOCKET_NO,SALE_TYPE,VEHICLE_MODEL,SALE_DATE,BOOKING_DATE,DELIVERY_DATE,
DEALER_NAME,ENQ_GEN_BY,EXEC_NAME,USER_CR,DATE_CR) SELECT '6','2','0','TEST',CONVERT(DATE,'01/05/2016',103),CONVERT(DATE,'10/05/2016',103),
CONVERT(DATE,'15/05/2016',103),'ABC','S I','V B','1',SWITCHOFFSET(SYSDATETIMEOFFSET(), '+05:30')
This is the create table query:
CREATE TABLE [dbo].[SaleNew](
[SALE_ID] [int] IDENTITY(1,1) NOT NULL,
[ENQ_AID] [bigint] NOT NULL,
[DOCKET_NO] [varchar](50) NOT NULL,
[VEHICLE_MODEL] [varchar](100) NOT NULL,
[SALE_DATE] [date] NULL,
[BOOKING_DATE] [date] NULL,
[DELIVERY_DATE] [date] NULL,
[DEALER_NAME] [date] NULL,
[ENQ_GEN_BY] [varchar](100) NULL,
[EXEC_NAME] [varchar](100) NULL,
[USER_CR] [int] NULL,
[DATE_CR] [date] NULL,
[USER_UP] [int] NULL,
[DATE_UP] [date] NULL,
[SALE_TYPE] [int] NOT NULL,CONSTRAINT [PK_SaleNew] PRIMARY KEY CLUSTERED (
[SALE_ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON ) ON [PRIMARY]
What could be the reason?
DEALER_NAME type is defined as DATE but you want to INSERT a VARCHAR type value ABC in it.
Execute following query in order to edit the mentioned column type, then Execute your INSERT query.
ALTER TABLE [dbo].[SaleNew]
ALTER COLUMN [DEALER_NAME] VARCHAR(50) NULL

Why is my simple SQL statement taking so long to execute and how do i go about finding the issue?

I have a very simple SQL query:
select o.Visit_ID
from Datamart.dbo.ww_Orders o
inner join Datamart.dbo.ww_Order_Details on o.Visit_ID = ww_Order_Details.Visit_ID
where o.runstamp = '20160422'
this query takes < 0 seconds to return 11173 rows
When I add the GROUP BY statement:
select o.Visit_ID
from Datamart.dbo.ww_Orders o
inner join Datamart.dbo.ww_Order_Details on o.Visit_ID = ww_Order_Details.Visit_ID
where o.runstamp = '20160422'
group by o.Visit_ID
the server takes 6min 30 sec to retrieve the 3047 rows.
I would expect the GROUP BY query to take not that much longer than the original. How do I go about finding what the issues are? thanks
here are the table definitions:
Orders:
CREATE TABLE [dbo].[ww_Orders](
[Visit_ID] [int] NOT NULL,
[Member_ID] [int] NOT NULL,
[Membership_no] [varchar](20) NULL,
[Member_Card_Num_Orig] [varchar](16) NULL,
[SCV_ID] [int] NULL,
[Meeting_No] [int] NULL,
[Location_Name] [varchar](128) NULL,
[Leader_No] [int] NULL,
[CashAmt] [decimal](18, 2) NULL,
[EFTAmt] [decimal](18, 2) NULL,
[VouchAmt] [decimal](18, 2) NULL,
[Meet_Date] [datetime] NULL,
[runstamp] [varchar](50) NULL,
CONSTRAINT [PK_dbo.ww_Orders] PRIMARY KEY CLUSTERED
(
[Visit_ID] 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
Order Details:
CREATE TABLE [dbo].[ww_Order_Details](
[ord_det_pk] [int] IDENTITY(1,1) NOT NULL,
[Visit_ID] [int] NOT NULL,
[Item_Code] [nvarchar](50) NULL,
[Item_Name] [nvarchar](50) NULL,
[Qty] [int] NULL,
[Amt] [decimal](18, 2) NULL,
[Category_Code] [nvarchar](20) NULL,
CONSTRAINT [PK_ww_Order_Details] PRIMARY KEY CLUSTERED
(
[ord_det_pk] 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
ALTER TABLE [dbo].[ww_Order_Details] WITH CHECK ADD CONSTRAINT [FK_ww_Order_Details_ww_Orders] FOREIGN KEY([Visit_ID])
REFERENCES [dbo].[ww_Orders] ([Visit_ID])
GO
ALTER TABLE [dbo].[ww_Order_Details] CHECK CONSTRAINT [FK_ww_Order_Details_ww_Orders]
GO
I'd add an index onto ww_Order_Details Visit_ID, probably make it the clustered index and drop the index on ord_det_pk. Also it might make more sense as an exists?
select o.Visit_ID
from Datamart.dbo.ww_Orders o
where exists (select 0 from Datamart.dbo.ww_Order_Details where o.Visit_ID = ww_Order_Details.Visit_ID)
and o.runstamp = '20160422'
I usually try to use a common table expression in cases like these, when a part of the query is really fast but an addition of a simple operation makes it really slow, usually it helps.
Try:
WITH CTE AS (
select o.Visit_ID
from Datamart.dbo.ww_Orders o
inner join Datamart.dbo.ww_Order_Details od on o.Visit_ID = od.Visit_ID
where o.runstamp = '20160422'
)
SELECT Visit_ID FROM CTE
group by Visit_ID
If this helps you can try to compare execution plans for your original query and this version to see whats going on