I have a table created by the following t-sql statement:
CREATE TABLE [Core].[PriceHistory](
[PriceHistoryId] [bigint] IDENTITY(1,1) NOT NULL,
[SourceId] [tinyint] NOT NULL,
[SymbolId] [smallint] NOT NULL,
[Ask] [real] NOT NULL,
[Bid] [real] NOT NULL,
[TickTime] [bigint] NOT NULL,
[ModifiedDate] [datetime2](3) NOT NULL,
[Direction] [tinyint] NULL,
CONSTRAINT [PK_PriceHistory] PRIMARY KEY CLUSTERED
(
[PriceHistoryId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Let's say I have a list of SymbolIds, for example (1, 2, 3).
I want to get the first rows foreach SymbolId having ModifiedDate is > than '2016-04-01 00:00:00'
SELECT *
FROM (
SELECT DENSE_RANK() OVER (
PARTITION BY SymbolId ORDER BY ModifiedDate
) RNK
,*
FROM PriceHistory
WHERE ModifiedDate > '2014-04-01 00:00:00'
) T
WHERE RNK = 1
Related
I have a temporal table defined as:
CREATE TABLE [dbo].[MyTable](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](200) NOT NULL,
[Description] [varchar](100) NULL,
[CreatedOn] [datetime] NULL,
[LastUpdatedOn] [datetime] NULL,
[SysStartTime] [datetime2](7) GENERATED ALWAYS AS ROW START NOT NULL,
[SysEndTime] [datetime2](7) GENERATED ALWAYS AS ROW END NOT NULL
CONSTRAINT [PK_MyTable] 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],
PERIOD FOR SYSTEM_TIME ([SysStartTime], [SysEndTime])
) ON [PRIMARY]
WITH
(
SYSTEM_VERSIONING = ON ( HISTORY_TABLE = [dbo].[MyTable_History] )
)
and when I try to do a query like this:
SELECT * FROM MyTable FOR SYSTEM_TIME from '2020-08-18 00:00:00' to '2020-08-18 23:59:59'
I seem to get rows that are not in the range as well. For example,
Name1 Description1 2018-07-05 22:53:36.543 2020-03-17 16:25:44.707 2020-07-22 18:21:03.9662004 9999-12-31 23:59:59.9999999
2020-07-22 is not in the range but why is it showing up?
I know this has been asked before, but none of the answers are fixing the issue, need another set of eyes. I'm trying to create a table and seed it with some values. I don't have any value constraints so I'm not sure why I'm getting the error I'm getting.
CREATE TABLE [dbo].[AvailableTime]
(
[Id] [INT] IDENTITY(1,1) NOT NULL,
[TimeString] [NCHAR] NOT NULL,
[TimeValue] [NCHAR] NOT NULL,
[CompanyId] [INT] NOT NULL,
[CompanyName] [NVARCHAR](MAX) NULL,
[LocationId] [INT] NOT NULL,
[LocationName] [NVARCHAR] NOT NULL,
[IsClaimed] [BIT] NOT NULL,
CONSTRAINT [PK_AvailableTime]
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
INSERT INTO dbo.AvailableTime (TimeString, TimeValue, CompanyId, CompanyName,
LocationId, LocationName, IsClaimed)
VALUES ('2019-01-07T00:00:00', '12:00 AM', 1, 'Company',
2, 'Inver Grove', 'FALSE');
You should never specify a CHAR, VARCHAR, NCHAR or NVARCHAR column (or variable, or parameter) without specifying an explicit length!
If you omit the specific length, in some cases you'll end up with variable or column of exactly ONE character of length! That's typically NOT what you want!
ALSO: why are you storing TimeString and TimeValue as NCHAR?? Doesn't make any sense - use the most appropriate datatype - and here, that would be DATETIME2(n) and TIME.
So define your table like this:
CREATE TABLE [dbo].[AvailableTime]
(
[Id] [INT] IDENTITY(1,1) NOT NULL,
[TimeString] DATETIM2(0) NOT NULL,
[TimeValue] TIME(0) NOT NULL,
[CompanyId] [INT] NOT NULL,
[CompanyName] [NVARCHAR](MAX) NULL,
[LocationId] [INT] NOT NULL,
[LocationName] [NVARCHAR](100) NOT NULL,
[IsClaimed] [BIT] NOT NULL,
and you should be fine.
You need declare the size of your CHAR & VARCHAR fields in the table:
CREATE TABLE [dbo].[AvailableTime]
(
[Id] [INT] IDENTITY(1,1) NOT NULL,
[TimeString] [NCHAR](50) NOT NULL,
[TimeValue] [NCHAR](50) NOT NULL,
[CompanyId] [INT] NOT NULL,
[CompanyName] [NVARCHAR](MAX) NULL,
[LocationId] [INT] NOT NULL,
[LocationName] [NVARCHAR](50) NOT NULL,
[IsClaimed] [BIT] NOT NULL,
CONSTRAINT [PK_AvailableTime]
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
You have a error in insert query, mismatch types in last column.
INSERT INTO dbo.AvailableTime (TimeString, TimeValue, CompanyId, CompanyName,
LocationId, LocationName, IsClaimed)
VALUES ('2019-01-07T00:00:00', '12:00 AM', 1, 'Company', 2, 'Inver Grove', 0);
The following is taking a long time to get executed. Considering each table has close to 50 million to 100 million rows, is there anything that I can do from the query side to get it optimized.It is a source table and involves huge data movements (insertion ranging in millions) every hour and is better off without indexes.I don't have the required access to get an execution plan from this server.
DECLARE #var INT
SELECT TOP 1 #var = s_type
FROM s_shot
WHERE s_type < 10000
ORDER BY s_type DESC;
WITH cte AS
(
SELECT
sps_id,
SUM(xin) xin,
SUM(xout) xout
FROM
s_lock
GROUP BY
sps_id
)
SELECT
A.[s_id], a.acc_id,
B.[o_type],
B.[o_id], B.[sec_id], B.[c_id],
B.[style_type_id], [s_type],
A.[end_date],
B.[mv],
b.xin + ISNULL(c.xin, 0) [xin],
b.xout + ISNULL(c.[xout], 0) xout,
b.accr_in + b.accr_inter [acc],
b.accr_in, b.accr_inter,
b.units
FROM
s_shot a WITH (NOLOCK)
JOIN
s_pos3 b WITH (NOLOCK) ON a.s_id = b.s_id
JOIN
cte c ON b.sps_id = c.sps_id
WHERE
b.is_sl = 1
AND a.end_date > DATEADD(mm, -24, GETDATE()) -- 24 months
As you can see, I want to fetch the data of last 24 months. Is there any optimization that is possible in this query so as to bring down its execution time to manageable levels.
The create table scripts are provided below.
CREATE TABLE [dbo].[s_shot]
(
[s_id] [int] IDENTITY(1,1) NOT NULL,
[acc_id] [int] NULL,
[s_type] [int] NULL,
[end_date] [datetime] NULL,
[recon] [tinyint] NULL,
[is_pers] [tinyint] NOT NULL,
CONSTRAINT [PK_s_shot_1] PRIMARY KEY NONCLUSTERED
(
[s_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]
CREATE TABLE [dbo].[s_lock]
(
[sps_id] [int] NOT NULL,
[units] [decimal](18, 5) NULL,
[accr_in] [decimal](18, 2) NULL,
[xin] [decimal](18, 2) NULL,
[xout] [decimal](18, 2) NULL,
[lock_date] [date] NOT NULL,
[accr_inter] [decimal](18, 2) NULL,
[mv] [decimal](18, 2) NULL,
CONSTRAINT [PK_s_lock] PRIMARY KEY CLUSTERED
(
[sps_id] ASC,
[lock_date] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[s_pos3]
(
[sps_id] [int] IDENTITY(1,1) NOT NULL,
[s_id] [int] NOT NULL,
[o_type] [tinyint] NOT NULL,
[o_id] [int] NOT NULL,
[s_id] [int] NOT NULL,
[c_id] [int] NOT NULL,
[s_type_id] [int] NOT NULL,
[units] [decimal](18, 5) NULL,
[accr_income] [decimal](18, 2) NULL,
[distr] [decimal](18, 2) NULL,
[mv] [decimal](18, 2) NULL,
[perf_stat] [smallint] NULL,
[is_slv] [bit] NULL,
[accr_inter] [decimal](18, 2) NULL,
[perf_mtd] [decimal](18, 10) NULL,
[xin] [decimal](18, 2) NULL,
[xout] [decimal](18, 2) NULL,
[a_s_type_id] [int] NULL,
CONSTRAINT [PK_s_pos3] PRIMARY KEY NONCLUSTERED
(
[sps_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]
Any help is appreciated.
Edit: Updated indexing details
I have a simple table:
CREATE TABLE DocModHistory
[ID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[DocID] [int] NOT NULL,
[BranchID] [int] NOT NULL,
[UserID] [int] NOT NULL,
[InsDate] [datetime] NOT NULL,
[Type] [int] NOT NULL,
CONSTRAINT [PK_DocModHistory] 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]
and I have two select queries:
SELECT dh.BranchID, MAX(dh.ID) as MaxID FROM DocModHistory dh WHERE dh.UserID = #p_UserID GROUP BY dh.BranchID
and
SELECT dh.DocID, MAX(dh.ID) as MaxID FROM DocModHistory dh WHERE dh.UserID = #p_UserID GROUP BY dh.DocID
Could you tell me please what indexes should I create?
Shall I create individual indexes for UserID, BranchID, DocID, or do I need multi-column indexes?
Thank you!
Create an index for UserID with Included columns BranchID, ID and DocID
So something like
CREATE INDEX IX_UserID ON DocModHistory (UserID) INCLUDE (BranchID, ID ,DocID);
I have three tables: LitHold, LitHoldDetails and EmailTemplate. The definitions are as follows.
CREATE TABLE [dbo].[LitHold](
[LitholdID] [int] IDENTITY(1,1) NOT NULL,
[LitHoldStatusID] [tinyint] NOT NULL,
[EmailReminderID] [tinyint] NULL,
[ApprovedDate] [datetime] NULL,
[TerminatedDate] [datetime] NULL,
CONSTRAINT [PK_Lithold] PRIMARY KEY CLUSTERED
(
[LitholdID] 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].[LitHoldDetails](
[LitHoldDetailsID] [int] IDENTITY(1,1) NOT NULL,
[LitholdID] [int] NOT NULL,
[VersionID] [int] NOT NULL,
[Description] [varchar](300) NULL,
[ResAttorneyID] [varchar](10) NOT NULL,
[Comments] [varchar](1000) NULL,
[HoldStartDate] [datetime] NULL,
[HoldEndDate] [datetime] NULL,
[CreatedDate] [datetime] NOT NULL,
[CreatedByLogin] [varchar](10) NULL,
CONSTRAINT [PK_LitholdDetails] PRIMARY KEY CLUSTERED
(
[LitHoldDetailsID] 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].[EmailTemplate](
[TemplateID] [int] IDENTITY(1,1) NOT NULL,
[LitHoldDetailsID] [int] NOT NULL,
[From] [varchar](50) NULL,
[To] [varchar](2000) NULL,
[CC] [varchar](500) NULL,
[BCC] [varchar](500) NULL,
[Subject] [nvarchar](200) NULL,
[MessageBody] [nvarchar](max) NULL,
[SendDate] [datetime] NULL,
[IsDefault] [bit] NOT NULL,
CONSTRAINT [PK_EmailTemplate] PRIMARY KEY CLUSTERED
(
[TemplateID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
For each LitHold, there can be multiple LitHoldDetails. For each LitHoldDetail, there should be one EmailTemplate. I recently found that some LitHoldDetails do NOT have EmailTemplates. We're still working in development on this project, so this isn't a big deal. However, we want to get the EmailTemplate data into the database. The situation is that for each LitHold, there is at least one LitHoldDetail that has an EmailTemplate. I would like to duplicate this EmailTemplate data for all the LitHoldDetails that a) have the same LitHoldID and b) do not have an EmailTemplate. One of the approaches I've tried is:
insert into EmailTemplate
(LitHoldDetailsID, [From], [To], CC, BCC, Subject, MessageBody, SendDate, IsDefault)
(select (select top 1 LitHoldDetailsID from LitHoldDetails where LitholdID = d.LitholdID and LitHoldDetailsID <> e.LitHoldDetailsID), [To], CC, BCC, Subject, MessageBody, SendDate, IsDefault from
EmailTemplate e inner join LitHoldDetails d on e.LitHoldDetailsID = d.LitHoldDetailsID)
but this gets me multiple rows for some LitHoldDetails, with different EmailTemplate data, and some rows where LitHoldDetails is NULL. How can I accomplish this? I'm using SQL Server 2008.
Try inserting this:
SELECT lhd.LitHoldDetailsID, CloneEmailTemplate.[From], ...
FROM LitHoldDetails lhd
CROSS APPLY (SELECT TOP 1 et.*
FROM EmailTemplate et
JOIN LitHoldDetails lhd2 ON lhd2.LitHoldDetailsID = et.LitHoldDetailsID
WHERE lhd2.LitHoldID = lhd.LitHoldID
) AS CloneEmailTemplate
WHERE NOT EXISTS (SELECT 1
FROM EmailTemplate et2
WHERE et2.LitHoldDetailsID = lhd.LitHoldDetailsID
)