I am facing a issue with latency when I select images which are saved in a varbinary(max) column in SQL Server, it takes around 3-5 mins to select at least 5 images. This table (galleryDetail) contains images with different sizes like 2mb, 40kb, 1mb likewise. As suggested in this link SQL server slow select query from type varbinary(max) (last comment),if somebody could give me a road map/suggestion, to achieve this task, it would be a big Help.
My table structure is as follows,
CREATE TABLE [dbo].[GalleryDetail]
(
[Id] [INT] IDENTITY(1,1) NOT NULL,
[Image] [VARBINARY](MAX) NULL,
[Title] [VARCHAR](250) NULL,
[Active] [BIT] NULL,
[CreatedDate] [DATETIME] NULL,
CONSTRAINT [PK_ImageGallery]
PRIMARY KEY CLUSTERED ([Id] ASC)
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[GalleryDetail]
ADD CONSTRAINT [DF_GalleryDetail_Active] DEFAULT ((1)) FOR [Active]
GO
If you select by Title column then add index to make select faster:
create index idx_Title on dbo.GalleryDetail(Title)
This only helps if Title is in the where clause of your query, e.g.
select * from GalleryDetail where Title = 'My picture'
or
select * from GalleryDetail where Title like 'picture%'
Related
I am having some trouble adding a table to my data entity model.
EntityFramework 6.1.3 installed
VS Enterprise 2017 15.9.3
Repro steps:
I open my Model.edmx
Right click > Update Model From Database
Add Table > select my table > Finish
And when I go into Model.Context.cs I see this line for my newly added table:
modelBuilder.Configurations.Add(new MyTable_Mapping());
but the MyTable_Mapping is underlined and complains the file is missing.
And inside Model.Mapping.cs reads
ErrorGeneratingOutput
My table schema:
CREATE TABLE [dbo].[MyTable](
[Id] [bigint] IDENTITY(1,1) NOT NULL,
[AccountUserId] [int] NOT NULL,
[MFAUserId] [bigint] NOT NULL,
[LastAuthDate] [datetime] NOT NULL,
[CreateDate] [datetime] NOT NULL,
[StatusId] [int] NOT NULL,
CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
I was working with VisualStudio 2017 C# and localdb2016 I created a table in server explorer's database desginer window with 10 columns added X rows.
I have already set copy if newer so rows wont get deleted everytime
but when i try to add a new column (11th column) all of my rows (even in other tables!) get deleted !
why ?
Here is my Table code generated from desginer .... :
CREATE TABLE [dbo].[CustomerTable] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[name] NVARCHAR (50) NULL,
[company] NVARCHAR (50) NULL,
[email] NVARCHAR (50) NULL,
[phone1] NVARCHAR (50) NULL,
[phone2] NVARCHAR (50) NULL,
[address] NVARCHAR (50) NULL,
[fax] NVARCHAR (50) NULL,
[phone3] NVARCHAR (50) NULL,
[date] DATETIME DEFAULT (getdate()) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
UPDATE 1 :
ALTER TABLE dbo.CustomerTable ADD column_b VARCHAR(20) NULL, column_c INT NULL ;
does that code remove old columns (b and c) if they exists?
is it a good idea to do that everytime app starts? (for upgrading purpose)
Try this Transact-SQL query
ALTER TABLE dbo.CustomerTable ADD column_b VARCHAR(20) NULL, column_c INT NULL ;
This query will add 2 columns in your table -> First, b (VARCHAR[20]) & Second, c (INT).
To read more,
ALTER TABLE (Transact-SQL)
The query will not remove any existing column because it is an alter query that means it alter the table as you mention. Adding existing column doesn't alter table. So, no changes.
I have got few tables in sql database, one of that contains names of others, but not all. I need to load names of tables (from that special one) as a list (xdocument), and using foreach and a simple query convert all of that to one xml file. database and app is on same azure account.
i have few table like that
CREATE TABLE [dbo].[table_name]
(
[P_ID] INT NOT NULL,
[name] NVARCHAR (255) NOT NULL,
PRIMARY KEY CLUSTERED ([P_ID] ASC)
);
and one with only varchar that contains
table_name1
table_name2
table_name4
i use that simple query
SELECT
P_ID "Table_name2/#id",
nazwa "Table_name2"
FROM Table_name2
FOR XML PATH('');
and get result, but only for one table
something
but i want a result for few tables in one xml using "file_converting_tables_to_xml.cs" in my app
<spinery>
<spiner title="Table_name1">
<wart id="1">black</wart>
</spiner>
<spiner title="Table_name3">
<wart id="1">white</wart>
</spiner>
or something similar
all that i found about it don't work for me,
so please help
If this was SQL Server you might try this:
--Creating two tables
CREATE TABLE [dbo].Table_name1
(
[P_ID] INT NOT NULL,
[name] NVARCHAR (255) NOT NULL,
PRIMARY KEY CLUSTERED ([P_ID] ASC)
);
CREATE TABLE [dbo].Table_name2
(
[P_ID] INT NOT NULL,
[name] NVARCHAR (255) NOT NULL,
PRIMARY KEY CLUSTERED ([P_ID] ASC)
);
GO
--Fill them with your sample data
INSERT INTO dbo.Table_name1 VALUES(1,'black');
INSERT INTO dbo.Table_name2 VALUES(1,'white');
GO
--Use UNION ALL to combine the results and FOR XML PATH to create the proper namings
SELECT 'Table_name1' AS [#title]
,P_ID [wart/#id]
,name [wart]
FROM Table_name1
UNION ALL
SELECT 'Table_name2' AS [#title]
,P_ID [wart/#id]
,name [wart]
FROM Table_name2
FOR XML PATH('spiner'),ROOT('spinery');
--Clean-Up (Carefull if real data!)
GO
--DROP TABLE dbo.Table_name1;
--DROP TABLE dbo.Table_name2;
I'm designing a .NET application where user can copy, edit, update or delete a record from a DataGrid element that is getting data from a table.
In the design I need to be able to maintain the versions to check all the changes that were made to the table by users. Can someone suggest what would be the best way to implement this requirement.
Thanks a lot.
As mentioned by Sean Lange, this is a complex topic and there is no silver bullet.
Change Data Capture is designed explicitly for this problem.
If you don't want to enable CDC, then triggers are usually your best bet.
Here is an example of an auditing trigger utilizing rowversion\timestamp:
CREATE TABLE [dbo].[Table1](
[Id] [int] NOT NULL,
[Data] [nvarchar](50) NULL,
[Version] [timestamp] NOT NULL,
CONSTRAINT [PK_Table1] 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]
GO
CREATE TABLE [dbo].[Table1_History](
[Id] [int] NOT NULL,
[Data] [nvarchar](50) NULL,
[Version] [binary](8) NOT NULL,
[ModDate] [datetimeoffset](7) NOT NULL,
[ModUser] [nvarchar](50) NOT NULL,
[Operation] CHAR(1) NOT NULL
) ON [PRIMARY]
GO
CREATE TRIGGER [dbo].[trgTable1_History]
ON [dbo].[Table1]
AFTER INSERT,DELETE,UPDATE
AS
BEGIN
SET NOCOUNT ON;
DECLARE #now DATETIMEOFFSET(7) = SYSDATETIMEOFFSET()
INSERT INTO dbo.Table1_History
(Id, Data, Version, ModDate, ModUser, Operation)
SELECT Id, Data, Version, #now, SYSTEM_USER, 'I' from inserted
INSERT INTO dbo.Table1_History
(Id, Data, Version, ModDate, ModUser, Operation)
SELECT Id, Data, Version, #now, SYSTEM_USER, 'D' from deleted
END
The timestamp column will automatically update upon every change to the row.
This provides you current + history in your audit table(which simplifies reporting). The Version column gives you an easy lookup between Table1 and Table1_History, in the event that you want to know the exact audit details of the current row. Updates are designated by a DELETE(D) and INSERT(I) occurring simultaneously in the audit.
If you are talking about a database table, then it's the best practice to actually never delete data from it. You should set a flag column in the table which denotes the operation performed in the corresponding row.
You can set it to 0 for active rows, 1 if the row is deleted. So, basically, you will perform an Update operation while deleting a row. Same can be followed for other CRUD operations.
This is my script in SQL but when I try use a form view in ASP.NET (visual studio). I worked with SQL Server Management Studio
CREATE TABLE [odl].[MaterialDetail]
(
[Material] [nvarchar](50) NOT NULL,
[PlantID] [nvarchar](50) NOT NULL,
[LabelPos] [char](1) NOT NULL,
[UseProdLabel] [tinyint] NOT NULL,
[UseContLabel] [tinyint] NOT NULL,
[UseTote] [tinyint] NOT NULL,
[COO] [nvarchar](50) NOT NULL,
[LabelsPerCont] [decimal](9, 2) NOT NULL,
[TareWeight] [decimal](9, 2) NOT NULL,
[AltTareWeight] [decimal](9, 2) NOT NULL,
[ModDate] [datetime] NOT NULL,
[CreateDate] [datetime] NOT NULL,
[rowguid] [uniqueidentifier] ROWGUIDCOL NOT NULL,
CONSTRAINT [PK_MaterialDetail] PRIMARY KEY CLUSTERED
(
[Material] ASC,
[PlantID] 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 [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_Material] DEFAULT ('') FOR [Material]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_PlantID] DEFAULT ('') FOR [PlantID]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_LabelPos] DEFAULT ('0') FOR [LabelPos]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_UseProdLabel] DEFAULT ((0)) FOR [UseProdLabel]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_UseContLabel] DEFAULT ((1)) FOR [UseContLabel]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_UseTote] DEFAULT ((0)) FOR [UseTote]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_COO] DEFAULT ('') FOR [COO]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_LabelPerCont] DEFAULT ((2)) FOR [LabelsPerCont]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_TareWeight] DEFAULT ((0)) FOR [TareWeight]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_AltTareWeight] DEFAULT ((0)) FOR [AltTareWeight]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_ModDate] DEFAULT (getdate()) FOR [ModDate]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_CreateDate] DEFAULT (getdate()) FOR [CreateDate]
GO
ALTER TABLE [odl].[MaterialDetail] ADD CONSTRAINT [DF_MaterialDetail_rowguid] DEFAULT (newsequentialid()) FOR [rowguid]
GO
I get this error in the moment when i try test my query
There was an error executing the query. Please check the syntax of
the command and if, present, the types and values of the parameters
and ensure they are correct Invalid object name 'MaterialDetail'
First of all, that SQL code doesn't produce any data or results. It just sets up a brand new empty table. I don't know what you'd expect your form-view to show, because there's not data here.
Secondly, if the table already exists in the database, or if the constraints you are adding already exist, I would expect this code to throw an error when run for a second time, because you are trying to create an object that already exists.
Finally, that SQL makes extensive use of the GO separator. GO is not a part of the SQL language. It is a feature of Sql Server Management Studio that is used for separating queries into different batches. The GO keyword is never sent to the database server, and Sql Server wouldn't know what to do with it if it were.