Insert to microsoft sql server user database failed - sql-server

Why I can't add more than one user to yii2 application? I'm using microsoft sql server and the signup code for yii2 app has not changed yet. When there is no record in user table, I can add user by using signup page from yii.
this is the DB script
USE [IBR]
GO
/****** Object: Table [dbo].[user] Script Date: 11/05/2016 16.51.02 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[user](
[id] [int] IDENTITY(1,1) NOT NULL,
[username] [nvarchar](255) NOT NULL,
[auth_key] [nvarchar](32) NOT NULL,
[password_hash] [nvarchar](255) NOT NULL,
[password_reset_token] [nvarchar](255) NULL,
[email] [nvarchar](255) NOT NULL,
[status] [smallint] NOT NULL DEFAULT ((10)),
[created_at] [int] NOT NULL,
[updated_at] [int] NOT NULL,
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],
UNIQUE NONCLUSTERED
(
[password_reset_token] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED
(
[email] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED
(
[username] 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
this is the screenshot.error while signup for 2nd times

Looks like your issue is with the [password_reset_token] column.
You have a unique constraint on that column but it allows NULL values and you're not inserting data in this statement, that's why it's seeing the NULL as a duplicate key. You can either remove the unique constraint on this field or look at inserting unique data into this field.

Related

SQL Server Full-Text Search not working on PDF File

My full text search works with doc and docx but not working with pdf. The filters have pdf fullpath: C:\WINDOWS\system32\Windows.Data.Pdf.dll
version: 6.2.19041.1023
Tell me what could be the matter?
CREATE TABLE [dbo].[FilesSearch](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[rConclusionCard] [bigint] NOT NULL,
[rConclusionCardFile] [bigint] NOT NULL,
[FileAgreementContent] [varbinary](max) NOT NULL,
[FileAgreementName] [nvarchar](255) NOT NULL,
[FileExt] AS (lower(reverse(substring(reverse([FileAgreementName]),(0),charindex('.',reverse([FileAgreementName]))+(1))))),
CONSTRAINT [PK_ASM_CONCLUSIONCARD_FILESSEA] 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] TEXTIMAGE_ON [PRIMARY]
GO
/****** Object: Index [UI_ConclusionCardFile] Script Date: 13.03.2022 22:07:57 ******/
CREATE UNIQUE NONCLUSTERED INDEX [UI_ConclusionCardFile] ON [dbo].[FilesSearch]
(
[id] 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]
GO
/****** Object: FullTextIndex Script Date: 13.03.2022 22:07:57 ******/
CREATE FULLTEXT INDEX ON [dbo].[FilesSearch](
[FileAgreementContent] TYPE COLUMN [FileExt] LANGUAGE 'Russian')
KEY INDEX [UI_ConclusionCardFile]ON ([FTConclusionFileSearch], FILEGROUP [PRIMARY])
WITH (CHANGE_TRACKING = AUTO, STOPLIST = SYSTEM)
CREATE FULLTEXT CATALOG [FTConclusionFileSearch] WITH ACCENT_SENSITIVITY = ON
AUTHORIZATION [dbo]
It seems that your FileExt column was a bit off, and was adding in the .. So remove the + 1 from it.
[FileExt] AS (lower(reverse(substring(reverse([FileAgreementName]),(0),charindex('.',reverse([FileAgreementName])))))),
Then rebuild the index
ALTER FULLTEXT CATALOG FTConclusionFileSearch REBUILD;

In Microsoft SQL Server, will 2 non clustered indices improve performance when there is already a non clustered composite index?

I have a table called Files that contains these columns
[Id] [int] IDENTITY(1,1) NOT NULL,
[RowVersion] [timestamp] NULL,
[CreatedAt] [datetime2](7) NOT NULL,
[UpdatedAt] [datetime2](7) NOT NULL,
[FileName] [nvarchar](450) NOT NULL,
[FileContent] [nvarchar](max) NULL,
[MessengerId] [int] NOT NULL,
[FileTypeId] [int] NOT NULL,
[Ref] [int] NOT NULL,
The table has a clustered index on the primary key Id.
Also there is a non clustered composite index on MessengerId and FileName
CREATE UNIQUE NONCLUSTERED INDEX [IX_Files_CourierId_FileName] ON [dbo].[Files]
(
[MessengerId] ASC,
[FileName] 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]
GO
This query is slow
SELECT COUNT(*) FROM Files WHERE MessengerId = 1 AND Filename = 'myfilename.xml'
I'm battling to test because the timeouts happen on a production server. On my developper laptop I have no issues.
Will adding 2 new indices on MessengerId and Filename improve performance?
The 2 new indices look like so
CREATE NONCLUSTERED INDEX [Index_on_FileName] ON [dbo].[Files]
(
[FileName] 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, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [Index_on_MessengerId] ON [dbo].[Files]
(
[MessengerId] 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, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
This query
SELECT COUNT(*) FROM Files WHERE MessengerId = 1 AND Filename = 'myfilename.xml'
requires a index equality-lookup on MessengerId and Filename and no other columns, so you current index will suffice.
You could equally switch the column order for the same result (you may find it a tiny bit faster in the event there is no match)
CREATE UNIQUE NONCLUSTERED INDEX [IX_Files_CourierId_FileName] ON [dbo].[Files]
(
[FileName] ASC,
[MessengerId] ASC
)
However the other two new indexes you propose do not cover the query, so the server most likely not even bother using them, especially if your existing query is still in place. Certainly they will be slower if used, as they will require key lookups to the clustered index.
Your actual timeout issue is more likely associated with locking because of other queries doing updates. I suggest you use a query to find any possible blockers, for example this one.

How to improve performance in SQL Server table with varbinary column?

I have a table in my database that we use as a filestore, the file itself is stored in a varbinary(MAX) column.
CREATE TABLE [dbo].[FILE_TABLE](
[FILE_ID] [int] IDENTITY(1,1) NOT NULL,
[HTML_FILE] [nvarchar](max) NULL,
[XML_FILE] [varbinary](max) NULL,
[FILE_EXTENSION] [nvarchar](50) NULL
CONSTRAINT [PK_FILE_TABLE] PRIMARY KEY CLUSTERED
(
[FILE_ID] 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] TEXTIMAGE_ON [PRIMARY]
GO
We also created the following non-clustered index:
CREATE NONCLUSTERED INDEX [NonClusteredIndex-20180220-000116] ON [dbo].[FILE_TABLE]
(
[FILE_ID] ASC
)
INCLUDE ( [XML_FILE]) 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
This table has semantics and freetext enabled to run a keyword search against the documents. When we run a simple freetext query, the performance is very slow. The table has 810 records only and it takes 30 seconds to return 713 rows. How could we improve the performance of this table or query?
SELECT * FROM FILE_TABLE WHERE freetext(XML_FILE,'lights')

aspnet_Users table with huge indexsize

We have an aspnet_Users table from aspnet membership table that shows up with
almost 18 gb index size.
rows 251172
datasize 56472 KB
indexsize 17800536 KB
This is just the standard aspnet membership table, but we do have an other table with a foreign key to this table (userid column).
Anyone seen this problem before?
How can i reduce the index size?
the aspnet_Users table is defined as
CREATE TABLE [dbo].[aspnet_Users](
[ApplicationId] [uniqueidentifier] NOT NULL,
[UserId] [uniqueidentifier] NOT NULL,
[UserName] [nvarchar](256) NOT NULL,
[LoweredUserName] [nvarchar](256) NOT NULL,
[MobileAlias] [nvarchar](16) NULL,
[IsAnonymous] [bit] NOT NULL,
[LastActivityDate] [datetime] NOT NULL,
PRIMARY KEY NONCLUSTERED
(
[UserId] 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 UNIQUE CLUSTERED INDEX [aspnet_Users_Index] ON [dbo].[aspnet_Users]
(
[ApplicationId] ASC,
[LoweredUserName] 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]
GO
CREATE NONCLUSTERED INDEX [aspnet_Users_Index2] ON [dbo].[aspnet_Users]
(
[ApplicationId] ASC,
[LastActivityDate] 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]
GO
ALTER TABLE [dbo].[aspnet_Users] ADD DEFAULT (newid()) FOR [UserId]
GO
ALTER TABLE [dbo].[aspnet_Users] ADD DEFAULT (NULL) FOR [MobileAlias]
GO
ALTER TABLE [dbo].[aspnet_Users] ADD DEFAULT ((0)) FOR [IsAnonymous]
GO
ALTER TABLE [dbo].[aspnet_Users] WITH NOCHECK ADD FOREIGN KEY([ApplicationId])
REFERENCES [dbo].[aspnet_Applications] ([ApplicationId])
GO
This works out at 70k per row though which is more then a single extent (8 pages, 64k) which implies massive fragmentation.
Have you ever run index maintenance on it? Run this and see what happens
ALTER INDEX ALL ON aspnet_Users REBUILD
Alternatively, has the table ever been extended with a LOB (pictures, XML etc) column? Or has someone added dozens of indexes? So please add the actual in-use table definition
Edit: remove the index on LastActivityDate, or change it to smalldatetime to keep minute accuracy, or only update it if changed more then xx seconds/minutes

Database Design - Preventing duplications for "Room" table

Hey everyone, I'm trying to create a database for a personal friend of mine and given my inexperience with developing databases I'm having difficulty trying to establish one I'm currently dealing with. Essentially, my is issue is with my "rooms" table which has an association with another table called "location"; The location is the is everything you would expect (buildingID, streetAddress,etc.), and Room has a foreign key containing the buildingId. I want my "rooms" table to have unique values for room numbers based on the buildingId.
To give you a clearer idea, I'll just c&p the script I'm using to create those tables.
CREATE TABLE [dbo].[Location](
[buildingId] [int] IDENTITY(1,1) NOT NULL,
[streetAddress] [varchar](50) NOT NULL,
[postalCode] [varchar](7) NOT NULL,
[province] [varchar](30) NOT NULL,
[city] [varchar](30) NOT NULL,
CONSTRAINT [PK_Location] PRIMARY KEY CLUSTERED
(
[buildingId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [UN_postalCode] UNIQUE NONCLUSTERED
(
[postalCode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [UN_streetAddress] UNIQUE NONCLUSTERED
(
[streetAddress] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[Room](
[rmId] [int] IDENTITY(1,1) NOT NULL,
[roomNum] [varchar](10) NOT NULL,
[floor] [int] NOT NULL,
[capacity] [int] NOT NULL,
[permission] [bit] NOT NULL,
[buildingId] [int] NOT NULL,
CONSTRAINT [PK_Room_1] PRIMARY KEY CLUSTERED
(
[rmId] 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].[Room] WITH CHECK ADD CONSTRAINT [FK_Room_Location] FOREIGN KEY([buildingId])
REFERENCES [dbo].[Location] ([buildingId])
GO
ALTER TABLE [dbo].[Room] CHECK CONSTRAINT [FK_Room_Location]
GO
Any help would greatly be appreciated.
Thanks.
A table level unique constraint?
ALTER TABLE dbo.Room WITH CHECK ADD
CONSTRAINT UQ_Room_RoomBuildingLocation UNIQUE (roomNum, buildingId)
This can be a unique index too which would allow INCLUDE columns

Resources