SQL 服务器连接中的 nvarchar 或 varchar 或日期
nvarchar or varchar or date in joins of SQL Server
我有一个使用 SQL Server 2008 R2 和 Windows 应用程序使用 SQL Server CE 4.0 在 Web 中开发的管理项目。
两者使用几乎相同的数据库结构。我的费用 table 为:
TABLE [fees]
(
[feeid] bigint NOT NULL IDENTITY(1,1),
[acno] int NULL DEFAULT 0,
[billingmonth] nvarchar(10) NULL,
[oldbal] numeric(18,2) NULL DEFAULT 0,
[duedtamt] numeric(18,2) NULL DEFAULT 0,
[afterdtamt] numeric(18,2) NULL DEFAULT 0,
[bal] numeric(18,2) NULL DEFAULT 0,
[depamt] numeric(18,2) NULL DEFAULT 0,
[totdpo] numeric(18,2) NULL DEFAULT 0,
[depdt] datetime NULL
)
billingmonth
将始终使用格式 MMM-yyyy
例如。 2018 年 1 月
BillingMonth
在其他 table 中使用各种连接(内连接和左外连接)。
如何提高 BillingMonth
的连接性能? ,我应该:
- 将 nvarchar 转换为 varchar(因为它将始终在 SQL2008R2 中存储月份)
- 将 nvarchar 转换为日期时间(在 SQL Server CE 和 SQL Server 2008 R2 中为 01-MMM-yyyy 月的第一天)
根本不建议将日期值存储为 nvarchar
有很多提高连接性能的建议:
- 使用
Date
数据类型
- 使用两个数字字段月和年而不是一个 varchar 字段(
tinyint
表示月份,smallint
表示年份,它们只能用于 加盟目的)
注意:正如@Pரதீப்提到的,当您分别存储月份和年份时,您需要在搜索日期范围时进行一些整数操作
我有一个使用 SQL Server 2008 R2 和 Windows 应用程序使用 SQL Server CE 4.0 在 Web 中开发的管理项目。
两者使用几乎相同的数据库结构。我的费用 table 为:
TABLE [fees]
(
[feeid] bigint NOT NULL IDENTITY(1,1),
[acno] int NULL DEFAULT 0,
[billingmonth] nvarchar(10) NULL,
[oldbal] numeric(18,2) NULL DEFAULT 0,
[duedtamt] numeric(18,2) NULL DEFAULT 0,
[afterdtamt] numeric(18,2) NULL DEFAULT 0,
[bal] numeric(18,2) NULL DEFAULT 0,
[depamt] numeric(18,2) NULL DEFAULT 0,
[totdpo] numeric(18,2) NULL DEFAULT 0,
[depdt] datetime NULL
)
billingmonth
将始终使用格式 MMM-yyyy
例如。 2018 年 1 月
BillingMonth
在其他 table 中使用各种连接(内连接和左外连接)。
如何提高 BillingMonth
的连接性能? ,我应该:
- 将 nvarchar 转换为 varchar(因为它将始终在 SQL2008R2 中存储月份)
- 将 nvarchar 转换为日期时间(在 SQL Server CE 和 SQL Server 2008 R2 中为 01-MMM-yyyy 月的第一天)
根本不建议将日期值存储为 nvarchar
有很多提高连接性能的建议:
- 使用
Date
数据类型 - 使用两个数字字段月和年而不是一个 varchar 字段(
tinyint
表示月份,smallint
表示年份,它们只能用于 加盟目的)
注意:正如@Pரதீப்提到的,当您分别存储月份和年份时,您需要在搜索日期范围时进行一些整数操作