简体   繁体   English

在SQL Server中存储大数据的最佳方法

[英]Best way to store large data in SQL Server

I have a news website created with ASP.NET MVC. 我有一个使用ASP.NET MVC创建的新闻网站。

Below is my table: 以下是我的表格:

CREATE TABLE [dbo].[News] (
[NewsID]         INT            IDENTITY (1, 1) NOT NULL,
[InstitutionID]  INT            NOT NULL,
[Title]          NVARCHAR (450) NOT NULL,
[NewsDate]       DATETIME       NOT NULL,
[Description]    NVARCHAR (MAX) NULL,
[path]           NVARCHAR (MAX) NULL,
[PostedBy]       NVARCHAR (MAX) NULL,
[ContactPhone]   NVARCHAR (MAX) NULL,
[ContactEmail]   NVARCHAR (MAX) NULL,
[NewsExpiryDate] DATETIME       NOT NULL,
CONSTRAINT [PK_dbo.News] PRIMARY KEY CLUSTERED ([NewsID] ASC),
CONSTRAINT [FK_dbo.News_dbo.state_InstitutionID] FOREIGN KEY ([InstitutionID]) REFERENCES [dbo].[state] ([InstitutionID]) ON DELETE CASCADE );

I noticed that my database has increased to 8GB, and it is getting slower to load a page. 我注意到我的数据库已增加到8GB,并且加载页面的速度越来越慢。

I am thinking of storing the news in a file and then saving the path in database. 我正在考虑将新闻存储在文件中,然后将路径保存在数据库中。

Is there anyway I can convert the form value representing "[Description]" into a file and then store the path to my database? 无论如何,我可以将代表“ [Description]”的表单值转换为文件,然后将路径存储到数据库中吗?

Certainly you can store files locally and then 'link' to them in the database. 当然,您可以将文件存储在本地,然后在数据库中“链接”到它们。 But that approach is probably the wrong one if your application and database are not on the same server (the database server should be optimized for file storage, and that's where files should go). 但是,如果您的应用程序和数据库不在同一服务器上,则这种方法可能是错误的方法(数据库服务器应针对文件存储进行优化,这就是文件存放的位置)。 First, You lose all querying and indexing possibilities if you store your Description field off-db. 首先,如果将Description字段存储在数据库外,则会失去所有查询和索引的可能性。 Second, the database server "should" be powerful enough for this task. 其次,数据库服务器“应该”足够强大以完成此任务。 You should look into tuning the database engine and server. 您应该研究调整数据库引擎和服务器。

Let's assume you've done all that. 假设您已完成所有操作。 You need to make sure your application (the thing connecting to the db) can write and read these files. 您需要确保您的应用程序(连接到数据库的东西)可以写入和读取这些文件。 You probably need to set up a top-level directory that has plenty of space. 您可能需要设置一个具有足够空间的顶级目录。 You should pay attention to permissions/security -- who can read/write these files. 您应该注意权限/安全性-谁可以读取/写入这些文件。 Finally, you need a scheme of storing files and creating these files. 最后,您需要一种存储文件并创建这些文件的方案。 A typical scheme is to convert the description to a hash (md5 would be OK here, sha256 would be best) and use the hash as the filename. 一种典型的方案是将描述转换为哈希(此处md5可以,而最好是sha256)并将哈希用作文件名。 You chop of the filename so that it contains 2 or 3 levels of directories (large directories often slow some systems down): So ab2442vc25 becomes a/b2/442vc25 . 您可以对文件名进行切碎,使其包含2或3级目录(大目录通常会使某些系统运行变慢):因此ab2442vc25变为a/b2/442vc25 Store your description there and store the path name in Description_path . 将描述存储在那里,并将路径名存储在Description_path Keep Description null in those cases. 在这种情况下,请将Description保持为空。

But I'm skeptical this will actually help. 但我怀疑这是否会有所帮助。

As John Saunders stated in the comments, you misuse nvarchar(max) . 约翰·桑德斯John Saunders )在评论中所述,您滥用nvarchar(max) If the Title can be stored in 450 chars, the ContactPhone can certainly be stored in less then 20. the PostedBy and Email also can be truncated to a more appropriate size, and I'm guessing the path also should not be over a 100 chars at most. 如果Title可以以450个字符存储,那么ContactPhone肯定可以以小于20个字符存储PostedByEmail也可以被截断为更合适的大小,我想path也不应该超过100个字符。最多。
Also, If possible, I would probably move the PostedBy , ContactPhone and ContactEmail to a new table (I would probably call it NewsAuthors ) and replace them in the News table by a foreign key to this table. 另外,如果可能,我可能PostedByContactPhoneContactEmail移到新表中(我可能将其NewsAuthors ),并用该表的外键将它们替换为News表。 This have the potential to save you a lot of storage space. 这有可能节省大量存储空间。

As to performance, storage size is usually not the problem, as NotMe suggested in the comments. 至于性能,存储大小通常不是问题,正如NotMe在评论中建议的那样。
You can probably improve performance dramatically by using proper queries and indexes on your database. 您可以通过在数据库上使用适当的查询和索引来显着提高性能。

A good place to start is running your most frequent queries directly on SSMS and view the execution plan. 一个不错的起点是直接在SSMS上运行最常见的查询并查看执行计划。 sql server may suggest creating indexes. sql server可能建议创建索引。 if it does, create them. 如果有,请创建它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM