简体   繁体   English

nvarchar或varchar或SQL Server联接中的日期

[英]nvarchar or varchar or date in joins of SQL Server

I have a management project developed in Web using SQL Server 2008 R2 and Windows Application using SQL Server CE 4.0. 我有一个使用SQL Server 2008 R2在Web上开发的管理项目和使用SQL Server CE 4.0的Windows应用程序。

Both uses almost same database structure. 两者使用几乎相同的数据库结构 I have a fees table as : 我有一张费用表:

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 will always use format MMM-yyyy eg. billingmonth将始终使用格式MMM-yyyy例如。 Jan-2018 扬2018

BillingMonth uses various joins (inner and left outer join) in other tables. BillingMonth在其他表中使用各种连接(内部和左外部连接)。

How to increase performance of joins with BillingMonth ? 如何使用BillingMonth提高连接性能? , should i : , 我是不是该 :

  • Convert nvarchar to varchar ( as it will always store Month in SQL2008R2 ) 将nvarchar转换为varchar(因为它将始终在SQL2008R2中存储Month)
  • Convert nvarchar to datetime ( as first day of month 01-MMM-yyyy in SQL Server CE and SQL Server 2008 R2) 将nvarchar转换为datetime(作为SQL Server CE和SQL Server 2008 R2中01-MMM-yyyy的第一天)

Storing date values as nvarchar is not recommended at all 根本不建议将日期值存储为nvarchar

There are many suggestions to increase the join performance: 有许多建议可以提高连接性能:

  • Use Date datatype 使用Date数据类型
  • Use two numeric field month and year instead of one varchar field ( tinyint for month, smallint for year, they can be used only for joining purpose ) 使用两个数字字段月份和年份而不是一个varchar字段tinyint表示月份, smallint表示年份,它们只能用于连接目的

Note that: AS @Pரதீப் mentioned, When you store month and year separately, you need to do some integer manipulations when searching for date ranges 需要注意的是:AS @Pரதீப்提到,当你分别存储月份和年份时,你需要在搜索日期范围时进行一些整数操作

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

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