SQL Azure is recommending Indexes that are already on my Table - sql

In the Azure Portal you see recommendations from SQL Azure. The weird thing is that these indexes already exist on my Table.
A query on MemberId on the Tip table also returns within I <250ms on a table of 7.5 million records so I have the feeling the index is working.
We do seem having performance problems during high traffic.
What could be going on?
TABLE
CREATE TABLE [dbo].[Tip](
[Id] [int] IDENTITY(1,1) NOT NULL,
[MemberId] [int] NOT NULL,
[Slot] [varchar](25) NOT NULL,
[PeriodKey] [varchar](25) NOT NULL,
[LastChanged] [datetime2](7) NOT NULL,
[Invalid] [bit] NOT NULL,
[Value] [nvarchar](50) NULL,
[ListingIndex] [tinyint] NOT NULL,
CONSTRAINT [PK_Tip] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY],
CONSTRAINT [IX_Tip_Unique_Key] UNIQUE NONCLUSTERED
(
[MemberId] ASC,
[PeriodKey] ASC,
[Slot] ASC,
[ListingIndex] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Tip] ADD CONSTRAINT [DF_Tip_Invalid] DEFAULT ((0)) FOR [Invalid]
GO
ALTER TABLE [dbo].[Tip] ADD CONSTRAINT [DF_Tip_ListingIndex] DEFAULT ((0)) FOR [ListingIndex]
GO
ALTER TABLE [dbo].[Tip] WITH NOCHECK ADD CONSTRAINT [FK_Tip_Member] FOREIGN KEY([MemberId])
REFERENCES [dbo].[Member] ([Id])
GO
ALTER TABLE [dbo].[Tip] NOCHECK CONSTRAINT [FK_Tip_Member]
GO
This is a really simple query on MemberId. Normal queries might be a bit more complicated:
This is the Index that it tries to create in the Azure portal:

Related

Join and/or Sub QUERY with Composite Key Tables

I asked a similar question a few days ago. I have three tables. The DDL for each table is found in this thread (SQL JOIN not returning any results). My needs have changed slightly (this is my shocked face) and I haven't had much success in getting to what I need; close, but not quite.
What I would like to do is hand in a users name (or part of there name, i.e. LIKE) and get back the Description (Variable.Value table) and the document filename (Documents table). Of the latest revision. I have done this to get the latest rev with great success. I have tried in a myriad of ways to plug in the authors name (variablevalue table) to get exactly what I am after. I have tried a subquery with no luck either. I think the composite keys are catching me out as I have not done a lot of work with composite keys as yet. TIA.
Select Distinct vv.ValueCache
from VariableValue vv, Documents d
where d.filename like 'ECO-%' and d.deleted = 0 and d.DocumentID = vv.DocumentID and
vv.VariableID = ( Select VariableID
from Variable
where VariableName like 'Description') and
vv.RevisionNo = ( Select Max( Vv1.RevisionNo )
From VariableValue vv1
where vv1.VariableID = vv.VariableID and
vv1.DocumentID = d.DocumentID and
vv1.ValueText is not null ) and
vv.ValueCache != ''
order by vv.ValueCache ASC
Here is the missing DDL for the Documents table.
USE [PDM_NT_Vault]
GO
/****** Object: Table [dbo].[Documents] Script Date: 9/29/2017 9:51:40 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Documents](
[DocumentID] [int] IDENTITY(1,1) NOT NULL,
[Filename] [nvarchar](255) NULL,
[LockProject] [int] NULL CONSTRAINT [DF__TemporaryU__Mode__3B75D760] DEFAULT ((2)),
[UserID] [int] NULL CONSTRAINT [DF__Temporary__UserI__3C69FB99] DEFAULT ((0)),
[LockDomain] [nvarchar](255) NULL,
[LockPath] [nvarchar](255) NULL,
[Busy] [bit] NOT NULL CONSTRAINT [DF__TemporaryU__Busy__3E52440B] DEFAULT ((0)),
[Flushed] [int] NULL CONSTRAINT [DF_Documents_Flushed] DEFAULT ((1)),
[DefValStored] [int] NULL CONSTRAINT [DF_Documents_DefValStored] DEFAULT ((0)),
[RevGenCounter] [int] NULL,
[LatestRevisionNo] [int] NULL,
[CurrentStatusID] [int] NULL,
[WorkingVersionModified] [int] NULL,
[ExtensionID] [int] NULL CONSTRAINT [DF_Documents_ExtensionID] DEFAULT ((1)),
[LockDate] [datetime] NULL,
[UserDocRefsModified] [bit] NULL,
[Deleted] [bit] NOT NULL CONSTRAINT [DF_Documents_Deleted] DEFAULT ((0)),
[Shared] [int] NULL CONSTRAINT [DF_Documents_Shared] DEFAULT ((0)),
[LockViewID] [uniqueidentifier] NULL,
[Link] [bit] NULL CONSTRAINT [DF_Documents_Link] DEFAULT ((0)),
[DocTypeID] [int] NULL,
[ObjectTypeID] [int] NULL CONSTRAINT [DF_Documents_ObjectTypeID] DEFAULT ((1)),
[Flags] [int] NOT NULL CONSTRAINT [DF_Documents_Flags] DEFAULT ((0)),
CONSTRAINT [aaaaaDocuments1_PK] PRIMARY KEY CLUSTERED
(
[DocumentID] 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
ALTER TABLE [dbo].[Documents] WITH NOCHECK ADD CONSTRAINT [Documents_FK00] FOREIGN KEY([UserID])
REFERENCES [dbo].[Users] ([UserID])
GO
ALTER TABLE [dbo].[Documents] CHECK CONSTRAINT [Documents_FK00]
GO
ALTER TABLE [dbo].[Documents] WITH NOCHECK ADD CONSTRAINT [FK_Documents_DocType] FOREIGN KEY([DocTypeID])
REFERENCES [dbo].[DocType] ([DocTypeID])
GO
ALTER TABLE [dbo].[Documents] CHECK CONSTRAINT [FK_Documents_DocType]
GO
ALTER TABLE [dbo].[Documents] WITH NOCHECK ADD CONSTRAINT [FK_Documents_FileExtension] FOREIGN KEY([ExtensionID])
REFERENCES [dbo].[FileExtension] ([ExtensionID])
GO
ALTER TABLE [dbo].[Documents] CHECK CONSTRAINT [FK_Documents_FileExtension]
GO
ALTER TABLE [dbo].[Documents] WITH NOCHECK ADD CONSTRAINT [FK_Documents_ObjectType] FOREIGN KEY([ObjectTypeID])
REFERENCES [dbo].[ObjectType] ([ObjectTypeID])
NOT FOR REPLICATION
GO
ALTER TABLE [dbo].[Documents] CHECK CONSTRAINT [FK_Documents_ObjectType]
GO
ALTER TABLE [dbo].[Documents] WITH CHECK ADD CONSTRAINT [FK_Documents_Projects1] FOREIGN KEY([LockProject])
REFERENCES [dbo].[Projects] ([ProjectID])
GO
ALTER TABLE [dbo].[Documents] CHECK CONSTRAINT [FK_Documents_Projects1]
GO

SQL - Add constraint with foreign key in another table

I try to add a constraint but I don't manage to do what I want :).
So in my case, I have these 3 tables :
[dbo].[User](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Language] [nvarchar](2) NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[Surname] [nvarchar](50) NOT NULL,
...
)
[dbo].[Project](
[Id] [int] NOT NULL,
[UserId] [int] NOT NULL,
[KindOfProjectId] [int] NOT NULL,
...
)
[dbo].[KindOfProject](
[Id] [int] NOT NULL,
[Language] [nvarchar](2) NOT NULL,
...
CONSTRAINT [PK__KindOfProject] PRIMARY KEY CLUSTERED
(
[Id] ASC,
[Language] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
The table KindOfProject has columns "Id" AND "Language" as Primary Key
And I have added a first constraint to link table Project with table User like this :
ALTER TABLE [dbo].[Project] WITH CHECK ADD CONSTRAINT [FK_Project_UserId] FOREIGN KEY([UserId])
REFERENCES [dbo].[User] ([Id])
Now, what I want to do is to add a constraint to link the column "KindOfProjectId" from table Project to the column "Id" from table KindOfProject :
ALTER TABLE [dbo].[Project] WITH CHECK ADD CONSTRAINT [FK_Project_KindOfProjectId] FOREIGN KEY([KindOfProjectId],[Language])
REFERENCES [dbo].[KindOfProject] ([Id],[Language])
But it does not work, as there is no column Language in table Project. But I can get it thanks to the link with the table User, as table User has a column Language.
So I have tried something like that :
ALTER TABLE [dbo].[Project] WITH CHECK ADD CONSTRAINT [FK_Project_KindOfProjectId] FOREIGN KEY([KindOfProjectId],[User].[Language])
REFERENCES [dbo].[KindOfProject] ([Id],[Language])
But nothing works.
Have you an idea about it ? :D
Thanks for your help !

Still FK conflict, even after investigation

4.857 million rows went through the flow OK, and only 38.000 rows sent to error output. This due to a FK conflict. ---> "The INSERT statement conflicted with the FOREIGN KEY constraint "FK_FactTransactions_DimCustomer".
The conflict occurred in database "", table "dbo.DimCustomer", column 'CustomerNr'.".
My problem is, after some investigation, i can't identify any conflict in the primary table dbo.dimcustomer.
Lets take Mid(Named CustomerNr in the DB) "60534658" for an example and let us see.
Picture NR
1: This is a data-view draft of some of the rows that got sent to error output for analysis.
2: This is the table where its supposed to insert, notice that rows with the same CustomerNr already exists, because some rows of the same CustomerNr, for a strange reason, got inserted while others did not
3: And last. This is the actual primary table(Customer table), where the Mid(CustomerNr) reference clearly exists!
Am i missing something here? why is it still in conflict?
ty for any answers!
TABLE STRUCTURE:
CREATE TABLE [dbo].[DimCustomer](
[CustomerNr] [int] NOT NULL,
[CustomerID] [int] NULL,
[GeographyKey] [int] NULL,
[OrgNum] [nvarchar](50) NULL,
[CustomerName] [nvarchar](255) NULL,
[Adress] [nvarchar](255) NULL,
[ZipCode] [nvarchar](255) NULL,
[MCC_Code] [float] NULL,
CONSTRAINT [PK_Dim.Customer_1] PRIMARY KEY CLUSTERED
(
[CustomerNr] 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].[DimCustomer] WITH CHECK ADD CONSTRAINT [FK_DimCustomer_DimGeography] FOREIGN KEY([GeographyKey])
REFERENCES [dbo].[DimGeography] ([GeographyKey])
GO
ALTER TABLE [dbo].[DimCustomer] CHECK CONSTRAINT [FK_DimCustomer_DimGeography]
GO
CREATE TABLE [dbo].[FactTransactions](
[TransactionKey] [int] IDENTITY(1,1) NOT NULL,
[Reportdate] [date] NULL,
[CustomerNr] [int] NULL,
[SchemeID] [smallint] NULL,
[PriceType] [int] NULL,
[Count] [int] NULL,
[Amount] [float] NULL,
[Commission] [float] NULL,
[InterchangeFee] [float] NULL,
[Currency] [nvarchar](3) NULL,
[FeeType] [int] NULL,
CONSTRAINT [PK_FactTransactions] PRIMARY KEY CLUSTERED
(
[TransactionKey] 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].[FactTransactions] WITH CHECK ADD CONSTRAINT [FK_FactTransactions_DimCardScheme] FOREIGN KEY([SchemeID])
REFERENCES [dbo].[DimCardScheme] ([SchemeID])
GO
ALTER TABLE [dbo].[FactTransactions] CHECK CONSTRAINT [FK_FactTransactions_DimCardScheme]
GO
ALTER TABLE [dbo].[FactTransactions] WITH CHECK ADD CONSTRAINT [FK_FactTransactions_DimCustomer] FOREIGN KEY([CustomerNr])
REFERENCES [dbo].[DimCustomer] ([CustomerNr])
GO
ALTER TABLE [dbo].[FactTransactions] CHECK CONSTRAINT [FK_FactTransactions_DimCustomer]
GO
ALTER TABLE [dbo].[FactTransactions] WITH CHECK ADD CONSTRAINT [FK_FactTransactions_DimDate] FOREIGN KEY([Reportdate])
REFERENCES [dbo].[DimDate] ([Date])
GO
ALTER TABLE [dbo].[FactTransactions] CHECK CONSTRAINT [FK_FactTransactions_DimDate]
GO
ALTER TABLE [dbo].[FactTransactions] WITH CHECK ADD CONSTRAINT [FK_FactTransactions_DimPriceType] FOREIGN KEY([PriceType])
REFERENCES [dbo].[DimPriceType] ([PriceType])
GO
ALTER TABLE [dbo].[FactTransactions] CHECK CONSTRAINT [FK_FactTransactions_DimPriceType]
GO
Example error output line:
2015-05-01,60534658,1,1,57,484,5280,3000000000002,78,340000000000003,0,EUR,57,1
Maybe you have some FactTrancsactions pointing to a customer which doesn't exist.
Try this statement to check if there are some transaction pointing to the wrong customer. Each row returned by this statement will bounce if you create the foreign key.
SELECT ft.*
FROM dbo.FactTransactions as ft
LEFT JOIN dbo.DimCustomer as dc
ON ft.CustomerNr = dc.CustomerNr
WHERE dc.CustomerNr IS NULL

Unable to drop foreign key constraint

Table definition:
CREATE TABLE [dbo].[Visualization_ComponentLayouts]
(
[ComponentLayoutId] [int] IDENTITY(1,1) NOT NULL,
[ComponentId] [int] NOT NULL,
[LayoutId] [int] NOT NULL,
[UnitTypeId] [int] NOT NULL,
[ProjectId] [int] NOT NULL,
[CreatedBy] [int] NULL,
[ModifiedBy] [int] NULL,
CONSTRAINT [PK_Visualization_ComponentLayouts] PRIMARY KEY CLUSTERED
(
[ComponentLayoutId] 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].[Visualization_ComponentLayouts] WITH NOCHECK
ADD CONSTRAINT [FK_Visualization_ComponentLayouts_Visualization_ComponentInfo]
FOREIGN KEY([ComponentId])
REFERENCES [dbo].[Visualization_ComponentInfo] ([ComponentId])
GO
ALTER TABLE [dbo].[Visualization_ComponentLayouts] CHECK CONSTRAINT [FK_Visualization_ComponentLayouts_Visualization_ComponentInfo]
GO
ALTER TABLE [dbo].[Visualization_ComponentLayouts] WITH CHECK
ADD CONSTRAINT [FK_Visualization_ComponentLayouts_Visualization_Layouts]
FOREIGN KEY([LayoutId], [ProjectId])
REFERENCES [dbo].[Visualization_Layouts] ([LayoutId], [ProjectId])
GO
ALTER TABLE [dbo].[Visualization_ComponentLayouts] CHECK CONSTRAINT [FK_Visualization_ComponentLayouts_Visualization_Layouts]
GO
Query to drop constraint:
ALTER TABLE Visualization_ComponentLayouts
DROP CONSTRAINT FK_Visualization_ComponentLayouts_Visualization_Layouts
Error I get when I execute above drop query:
'FK_Visualization_ComponentLayouts_Visualization_Layouts' is not a
constraint. Could not drop constraint. See previous errors.
In SQL Server Management Studio, in Object Explorer, when I expand the table node, and within that when I expand constraints node, no constraints are being listed there.
When I execute query : EXEC sp_help 'Visualization_ComponentLayouts'
I get following:
What am I doing wrong?

Error creating In-Memory Tables

I am trying to create a table, in-memory but it keeps giving me errors like:
Msg 10794, Level 16, State 85, Line 11 The index option 'pad_index' is
not supported with indexes on memory optimized tables.
and another one saying the same thing about Primary Key Clustered.
Those two things above (pad_index and PK Clustered) are needed, unless there is another way to get it to work...
[DEMO-Training1]
GO
/****** Object: Table [dbo].[wtAssetChildren] ******/
SET ANSI_NULLS ON GO
SET QUOTED_IDENTIFIER ON GO
CREATE TABLE [dbo].[wtAssetChildren] (
[wtAssetChildrenID] [int] IDENTITY(1,1) NOT NULL,
[wtGUID] [uniqueidentifier] NOT NULL,
[CallingAssetID] [int] NOT NULL,
[AssetID] [int] NOT NULL,
[Processed] [bit] NOT NULL,
CONSTRAINT [PK_wtAssetChildren] PRIMARY KEY CLUSTERED ([wtAssetChildrenID] 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].[wtAssetChildren]
ADD CONSTRAINT [DF_wtAssetChildren_Processed] DEFAULT ((0)) FOR [Processed] GO
In-Memory OLTP Tables have a bunch of restrictions relative to the rest of what generally ships with MS SQL. Check out http://msdn.microsoft.com/en-us/library/dn246937.aspx for a full list.
Clustered PKs are not supported, for example. You'll have to use nonclustered primary key indices instead.
Below is an example of how you can create this table in a memory-optimized SQL Server 2014. Note that just because you can, doesn't mean you should. You mention you are beginner and memory-optimized tables are more of an advanced option.
CREATE DATABASE MemoryOptimizedTest;
GO
ALTER DATABASE MemoryOptimizedTest
ADD FILEGROUP MemoryOptimizedTest_MemoryOptimized
CONTAINS MEMORY_OPTIMIZED_DATA;
GO
ALTER DATABASE MemoryOptimizedTest
ADD FILE (
NAME = 'MemoryOptimizedTest__MemoryOptimized1'
, FILENAME = 'D:\SqlDataFiles\MemoryOptimizedTest__MemoryOptimized1')
TO FILEGROUP MemoryOptimizedTest_MemoryOptimized;
GO
ALTER DATABASE MemoryOptimizedTest
SET MEMORY_OPTIMIZED_ELEVATE_TO_SNAPSHOT=ON;
GO
USE MemoryOptimizedTest;
GO
CREATE TABLE [dbo].[wtAssetChildren](
[wtAssetChildrenID] [int] IDENTITY(1,1) NOT NULL
, [wtGUID] [uniqueidentifier] NOT NULL
, [CallingAssetID] [int] NOT NULL
, [AssetID] [int] NOT NULL
, [Processed] [bit] NOT NULL
CONSTRAINT [DF_wtAssetChildren_Processed] DEFAULT ((0))
, CONSTRAINT [PK_wtAssetChildren] PRIMARY KEY NONCLUSTERED( [wtAssetChildrenID] ASC )
)
WITH (MEMORY_OPTIMIZED=ON, DURABILITY=SCHEMA_AND_DATA);