Friday, March 30, 2012
Help Optimize my Query
how am I to provide the indices? fill factor?
SELECT TOP 250 dbo.IHLEAktionenPlan.AktionenPlanID,
dbo.IHLEAktionenPlan.AdressID, dbo.IHLEMassnahmeStamm.Bezeichnung AS
Massnahme,
dbo.IHLEAktionenStamm.Name AS Aktion,
dbo.IHLEAdressenStamm.Name1 AS KundenName,
dbo.IHLEAktionenPlan.GeplanteAusfuehrung,
dbo.IHLEAktionenPlan.GueltigBis,
dbo.IHLEAktionenPlan.Wiedervorlage, dbo.IHLEAktionenPlan.WiedervorlageText,
dbo.IHLEAktionenPlan.NachbearbeitungsCodeID,
dbo.IHLEMassnahmePlan.Statuspruefung AS StatusPruefung,
dbo.IHLEMarketingParameter.StatusKennzeichen AS
StatusKennzeichen, dbo.IHLEMarketingParameter.Leistungsangebote AS
LeistungsAngebote,
dbo.IHLEMassnahmePlan.LeistungsAngebotPruefung AS
LeistungsAngebotPruefung,
dbo.IHLEMarketingParameter.Kommunikationsart AS
KommunikationsArt,
dbo.IHLEMassnahmePlan.KommunikationsArtPruefung AS
KommunikationsArtPruefung
FROM dbo.IHLEAktionenPlan INNER JOIN
dbo.IHLEMassnahmeStamm ON
dbo.IHLEAktionenPlan.MassnahmeID = dbo.IHLEMassnahmeStamm.MassnahmeID INNER
JOIN
dbo.IHLEAktionenStamm ON dbo.IHLEAktionenPlan.AktionID
= dbo.IHLEAktionenStamm.AktionID INNER JOIN
dbo.IHLEMassnahmePlan ON
dbo.IHLEAktionenPlan.MassnahmePlanID = dbo.IHLEMassnahmePlan.MassnahmePlanID
INNER JOIN
dbo.IHLEAdressenStamm ON dbo.IHLEAktionenPlan.AdressID
= dbo.IHLEAdressenStamm.AdressID INNER JOIN
dbo.IHLEMarketingParameter ON
dbo.IHLEAktionenPlan.AdressID = dbo.IHLEMarketingParameter.AdressID
WHERE (dbo.IHLEAktionenPlan.AusgefuehrtAm IS NOT NULL) AND
(dbo.IHLEMassnahmeStamm.LandID = @.LANDID) AND
(dbo.IHLEAktionenPlan.AusgefuehrtAm >= @.DATEFROM) AND
(dbo.IHLEAktionenPlan.AusgefuehrtAm <= @.DATETO)
ORDER BY dbo.IHLEAktionenPlan.Wiedervorlage,
dbo.IHLEAktionenPlan.GeplanteAusfuehrungHi
Have you looked at an execution plan? Does an optimizer available to use
indexes defined on the tables?
"mPiccoli" <mPiccoli@.discussions.microsoft.com> wrote in message
news:FA9C59EB-0FD4-4850-9A6D-51F848BEDE8F@.microsoft.com...
> that takes 20 seconds may however maximally 5 last
> how am I to provide the indices? fill factor?
> SELECT TOP 250 dbo.IHLEAktionenPlan.AktionenPlanID,
> dbo.IHLEAktionenPlan.AdressID, dbo.IHLEMassnahmeStamm.Bezeichnung AS
> Massnahme,
> dbo.IHLEAktionenStamm.Name AS Aktion,
> dbo.IHLEAdressenStamm.Name1 AS KundenName,
> dbo.IHLEAktionenPlan.GeplanteAusfuehrung,
> dbo.IHLEAktionenPlan.GueltigBis,
> dbo.IHLEAktionenPlan.Wiedervorlage,
dbo.IHLEAktionenPlan.WiedervorlageText,
> dbo.IHLEAktionenPlan.NachbearbeitungsCodeID,
> dbo.IHLEMassnahmePlan.Statuspruefung AS StatusPruefung,
> dbo.IHLEMarketingParameter.StatusKennzeichen AS
> StatusKennzeichen, dbo.IHLEMarketingParameter.Leistungsangebote AS
> LeistungsAngebote,
> dbo.IHLEMassnahmePlan.LeistungsAngebotPruefung AS
> LeistungsAngebotPruefung,
> dbo.IHLEMarketingParameter.Kommunikationsart AS
> KommunikationsArt,
> dbo.IHLEMassnahmePlan.KommunikationsArtPruefung AS
> KommunikationsArtPruefung
> FROM dbo.IHLEAktionenPlan INNER JOIN
> dbo.IHLEMassnahmeStamm ON
> dbo.IHLEAktionenPlan.MassnahmeID = dbo.IHLEMassnahmeStamm.MassnahmeID
INNER
> JOIN
> dbo.IHLEAktionenStamm ON
dbo.IHLEAktionenPlan.AktionID
> = dbo.IHLEAktionenStamm.AktionID INNER JOIN
> dbo.IHLEMassnahmePlan ON
> dbo.IHLEAktionenPlan.MassnahmePlanID =
dbo.IHLEMassnahmePlan.MassnahmePlanID
> INNER JOIN
> dbo.IHLEAdressenStamm ON
dbo.IHLEAktionenPlan.AdressID
> = dbo.IHLEAdressenStamm.AdressID INNER JOIN
> dbo.IHLEMarketingParameter ON
> dbo.IHLEAktionenPlan.AdressID = dbo.IHLEMarketingParameter.AdressID
> WHERE (dbo.IHLEAktionenPlan.AusgefuehrtAm IS NOT NULL) AND
> (dbo.IHLEMassnahmeStamm.LandID = @.LANDID) AND
> (dbo.IHLEAktionenPlan.AusgefuehrtAm >= @.DATEFROM)
AND
> (dbo.IHLEAktionenPlan.AusgefuehrtAm <= @.DATETO)
> ORDER BY dbo.IHLEAktionenPlan.Wiedervorlage,
> dbo.IHLEAktionenPlan.GeplanteAusfuehrung|||Did you look at the joined and the condition columns, are they indexed ?
Thats would be the main improment.
Jens SUessmeyer.
"mPiccoli" <mPiccoli@.discussions.microsoft.com> schrieb im Newsbeitrag
news:FA9C59EB-0FD4-4850-9A6D-51F848BEDE8F@.microsoft.com...
> that takes 20 seconds may however maximally 5 last
> how am I to provide the indices? fill factor?
> SELECT TOP 250 dbo.IHLEAktionenPlan.AktionenPlanID,
> dbo.IHLEAktionenPlan.AdressID, dbo.IHLEMassnahmeStamm.Bezeichnung AS
> Massnahme,
> dbo.IHLEAktionenStamm.Name AS Aktion,
> dbo.IHLEAdressenStamm.Name1 AS KundenName,
> dbo.IHLEAktionenPlan.GeplanteAusfuehrung,
> dbo.IHLEAktionenPlan.GueltigBis,
> dbo.IHLEAktionenPlan.Wiedervorlage,
> dbo.IHLEAktionenPlan.WiedervorlageText,
> dbo.IHLEAktionenPlan.NachbearbeitungsCodeID,
> dbo.IHLEMassnahmePlan.Statuspruefung AS StatusPruefung,
> dbo.IHLEMarketingParameter.StatusKennzeichen AS
> StatusKennzeichen, dbo.IHLEMarketingParameter.Leistungsangebote AS
> LeistungsAngebote,
> dbo.IHLEMassnahmePlan.LeistungsAngebotPruefung AS
> LeistungsAngebotPruefung,
> dbo.IHLEMarketingParameter.Kommunikationsart AS
> KommunikationsArt,
> dbo.IHLEMassnahmePlan.KommunikationsArtPruefung AS
> KommunikationsArtPruefung
> FROM dbo.IHLEAktionenPlan INNER JOIN
> dbo.IHLEMassnahmeStamm ON
> dbo.IHLEAktionenPlan.MassnahmeID = dbo.IHLEMassnahmeStamm.MassnahmeID
> INNER
> JOIN
> dbo.IHLEAktionenStamm ON
> dbo.IHLEAktionenPlan.AktionID
> = dbo.IHLEAktionenStamm.AktionID INNER JOIN
> dbo.IHLEMassnahmePlan ON
> dbo.IHLEAktionenPlan.MassnahmePlanID =
> dbo.IHLEMassnahmePlan.MassnahmePlanID
> INNER JOIN
> dbo.IHLEAdressenStamm ON
> dbo.IHLEAktionenPlan.AdressID
> = dbo.IHLEAdressenStamm.AdressID INNER JOIN
> dbo.IHLEMarketingParameter ON
> dbo.IHLEAktionenPlan.AdressID = dbo.IHLEMarketingParameter.AdressID
> WHERE (dbo.IHLEAktionenPlan.AusgefuehrtAm IS NOT NULL) AND
> (dbo.IHLEMassnahmeStamm.LandID = @.LANDID) AND
> (dbo.IHLEAktionenPlan.AusgefuehrtAm >= @.DATEFROM) AND
> (dbo.IHLEAktionenPlan.AusgefuehrtAm <= @.DATETO)
> ORDER BY dbo.IHLEAktionenPlan.Wiedervorlage,
> dbo.IHLEAktionenPlan.GeplanteAusfuehrung|||First of all, When you have a query this long, with table and column names a
s
long as this, use aliases as much as possible to improve readability.
Example: Your
"mPiccoli" wrote:
> that takes 20 seconds may however maximally 5 last
> how am I to provide the indices? fill factor?
> SELECT TOP 250 dbo.IHLEAktionenPlan.AktionenPlanID,
> dbo.IHLEAktionenPlan.AdressID, dbo.IHLEMassnahmeStamm.Bezeichnung AS
> Massnahme,
> dbo.IHLEAktionenStamm.Name AS Aktion,
> dbo.IHLEAdressenStamm.Name1 AS KundenName,
> dbo.IHLEAktionenPlan.GeplanteAusfuehrung,
> dbo.IHLEAktionenPlan.GueltigBis,
> dbo.IHLEAktionenPlan.Wiedervorlage, dbo.IHLEAktionenPlan.WiedervorlageText
,
> dbo.IHLEAktionenPlan.NachbearbeitungsCodeID,
> dbo.IHLEMassnahmePlan.Statuspruefung AS StatusPruefung,
> dbo.IHLEMarketingParameter.StatusKennzeichen AS
> StatusKennzeichen, dbo.IHLEMarketingParameter.Leistungsangebote AS
> LeistungsAngebote,
> dbo.IHLEMassnahmePlan.LeistungsAngebotPruefung AS
> LeistungsAngebotPruefung,
> dbo.IHLEMarketingParameter.Kommunikationsart AS
> KommunikationsArt,
> dbo.IHLEMassnahmePlan.KommunikationsArtPruefung AS
> KommunikationsArtPruefung
> FROM dbo.IHLEAktionenPlan INNER JOIN
> dbo.IHLEMassnahmeStamm ON
> dbo.IHLEAktionenPlan.MassnahmeID = dbo.IHLEMassnahmeStamm.MassnahmeID INNE
R
> JOIN
> dbo.IHLEAktionenStamm ON dbo.IHLEAktionenPlan.Aktion
ID
> = dbo.IHLEAktionenStamm.AktionID INNER JOIN
> dbo.IHLEMassnahmePlan ON
> dbo.IHLEAktionenPlan.MassnahmePlanID = dbo.IHLEMassnahmePlan.MassnahmePlan
ID
> INNER JOIN
> dbo.IHLEAdressenStamm ON dbo.IHLEAktionenPlan.Adress
ID
> = dbo.IHLEAdressenStamm.AdressID INNER JOIN
> dbo.IHLEMarketingParameter ON
> dbo.IHLEAktionenPlan.AdressID = dbo.IHLEMarketingParameter.AdressID
> WHERE (dbo.IHLEAktionenPlan.AusgefuehrtAm IS NOT NULL) AND
> (dbo.IHLEMassnahmeStamm.LandID = @.LANDID) AND
> (dbo.IHLEAktionenPlan.AusgefuehrtAm >= @.DATEFROM) AN
D
> (dbo.IHLEAktionenPlan.AusgefuehrtAm <= @.DATETO)
> ORDER BY dbo.IHLEAktionenPlan.Wiedervorlage,
> dbo.IHLEAktionenPlan.GeplanteAusfuehrung|||First of all, to suggest what indices should exist, it would be good to know
the structure of the tables involved, and the DRI (Declared Referential
Integrity) constraints on the tables... Post the DDL...
Next, When you have a query this long, with table and column names as
long as this, use aliases as much as possible, and indent the major clauses
of teh SQL, to improve readability.
for Example:
The following is identical to your query
SELECT TOP 250 Ap.AktionenPlanID,
Ap.AdressID, MS.Bezeichnung Massnahme,
Ak.Name Aktion, Ad.Name1 KundenName,
Ap.GeplanteAusfuehrung, Ap.GueltigBis,
Ap.Wiedervorlage, Ap.WiedervorlageText,
Ap.NachbearbeitungsCodeID,
Mp.Statuspruefung StatusPruefung,
Mk.StatusKennzeichen StatusKennzeichen,
Mk.Leistungsangebote LeistungsAngebote,
Mp.LeistungsAngebotPruefung LeistungsAngebotPruefung,
Mk.Kommunikationsart KommunikationsArt,
Mp.KommunikationsArtPruefung KommunikationsArtPruefung
FROM dbo.IHLEAktionenPlan Ap
JOIN dbo.IHLEMassnahmeStamm Ms
ON Ms.MassnahmeID = Ap.MassnahmeID
JOIN dbo.IHLEAktionenStamm Ak
ON Ak.AktionID = Ap.AktionID
JOIN dbo.IHLEAktionenStamm Mp
ON Mp.MassnahmePlanID = Ap.MassnahmePlanID
JOIN dbo.IHLEAdressenStamm Ad
ON Ad.AdressID = Ap.AdressID
JOIN dbo.IHLEMarketingParameter Mk
ON Mk.AdressID = Ap.AdressID =
WHERE Ap.AusgefuehrtAm IS NOT NULL
AND MS.LandID = @.LANDID
AND Ap.AusgefuehrtAm >= @.DATEFROM
AND Ap.AusgefuehrtAm <= @.DATETO
ORDER BY Ap.Wiedervorlage, Ap.GeplanteAusfuehrung
"mPiccoli" wrote:
> that takes 20 seconds may however maximally 5 last
> how am I to provide the indices? fill factor?
> SELECT TOP 250 dbo.IHLEAktionenPlan.AktionenPlanID,
> dbo.IHLEAktionenPlan.AdressID, dbo.IHLEMassnahmeStamm.Bezeichnung AS
> Massnahme,
> dbo.IHLEAktionenStamm.Name AS Aktion,
> dbo.IHLEAdressenStamm.Name1 AS KundenName,
> dbo.IHLEAktionenPlan.GeplanteAusfuehrung,
> dbo.IHLEAktionenPlan.GueltigBis,
> dbo.IHLEAktionenPlan.Wiedervorlage, dbo.IHLEAktionenPlan.WiedervorlageText
,
> dbo.IHLEAktionenPlan.NachbearbeitungsCodeID,
> dbo.IHLEMassnahmePlan.Statuspruefung AS StatusPruefung,
> dbo.IHLEMarketingParameter.StatusKennzeichen AS
> StatusKennzeichen, dbo.IHLEMarketingParameter.Leistungsangebote AS
> LeistungsAngebote,
> dbo.IHLEMassnahmePlan.LeistungsAngebotPruefung AS
> LeistungsAngebotPruefung,
> dbo.IHLEMarketingParameter.Kommunikationsart AS
> KommunikationsArt,
> dbo.IHLEMassnahmePlan.KommunikationsArtPruefung AS
> KommunikationsArtPruefung
> FROM dbo.IHLEAktionenPlan INNER JOIN
> dbo.IHLEMassnahmeStamm ON
> dbo.IHLEAktionenPlan.MassnahmeID = dbo.IHLEMassnahmeStamm.MassnahmeID INNE
R
> JOIN
> dbo.IHLEAktionenStamm ON dbo.IHLEAktionenPlan.Aktion
ID
> = dbo.IHLEAktionenStamm.AktionID INNER JOIN
> dbo.IHLEMassnahmePlan ON
> dbo.IHLEAktionenPlan.MassnahmePlanID = dbo.IHLEMassnahmePlan.MassnahmePlan
ID
> INNER JOIN
> dbo.IHLEAdressenStamm ON dbo.IHLEAktionenPlan.Adress
ID
> = dbo.IHLEAdressenStamm.AdressID INNER JOIN
> dbo.IHLEMarketingParameter ON
> dbo.IHLEAktionenPlan.AdressID = dbo.IHLEMarketingParameter.AdressID
> WHERE (dbo.IHLEAktionenPlan.AusgefuehrtAm IS NOT NULL) AND
> (dbo.IHLEMassnahmeStamm.LandID = @.LANDID) AND
> (dbo.IHLEAktionenPlan.AusgefuehrtAm >= @.DATEFROM) AN
D
> (dbo.IHLEAktionenPlan.AusgefuehrtAm <= @.DATETO)
> ORDER BY dbo.IHLEAktionenPlan.Wiedervorlage,
> dbo.IHLEAktionenPlan.GeplanteAusfuehrung|||my indices:
PK – AktionenPlanID
IX – AdressID
IX1 – MassnahmeID,AktionID
IX2 – LandID,AusgefuehrtAm
IX3 – Wiedervorlage, GeplanteAusfuerhung
the table contains 1.500.000 data records
if I order by cluse do not use then takes only 4 seconds
can someone explain to me why?
"Jens Sü?meyer" wrote:
> Did you look at the joined and the condition columns, are they indexed ?
> Thats would be the main improment.
> Jens SUessmeyer.
>
> "mPiccoli" <mPiccoli@.discussions.microsoft.com> schrieb im Newsbeitrag
> news:FA9C59EB-0FD4-4850-9A6D-51F848BEDE8F@.microsoft.com...
>
>|||my indices:
PK – AktionenPlanID
IX – AdressID
IX1 – MassnahmeID,AktionID
IX2 – LandID,AusgefuehrtAm
IX3 – Wiedervorlage, GeplanteAusfuerhung
the table contains 1.500.000 data records
if I order by cluse do not use then takes only 4 seconds
can someone explain to me why?
"Jens Sü?meyer" wrote:
> Did you look at the joined and the condition columns, are they indexed ?
> Thats would be the main improment.
> Jens SUessmeyer.
>
> "mPiccoli" <mPiccoli@.discussions.microsoft.com> schrieb im Newsbeitrag
> news:FA9C59EB-0FD4-4850-9A6D-51F848BEDE8F@.microsoft.com...
>
>|||my indices:
PK – AktionenPlanID
IX – AdressID
IX1 – MassnahmeID,AktionID
IX2 – LandID,AusgefuehrtAm
IX3 – Wiedervorlage, GeplanteAusfuerhung
the table contains 1.500.000 data records
if I order by cluse do not use then takes only 4 seconds
can someone explain to me why?
"Uri Dimant" wrote:
> Hi
> Have you looked at an execution plan? Does an optimizer available to use
> indexes defined on the tables?
> "mPiccoli" <mPiccoli@.discussions.microsoft.com> wrote in message
> news:FA9C59EB-0FD4-4850-9A6D-51F848BEDE8F@.microsoft.com...
> dbo.IHLEAktionenPlan.WiedervorlageText,
> INNER
> dbo.IHLEAktionenPlan.AktionID
> dbo.IHLEMassnahmePlan.MassnahmePlanID
> dbo.IHLEAktionenPlan.AdressID
> AND
>
>|||my indices:
PK – AktionenPlanID
IX – AdressID
IX1 – MassnahmeID,AktionID
IX2 – LandID,AusgefuehrtAm
IX3 – Wiedervorlage, GeplanteAusfuerhung
the table contains 1.500.000 data records
if I order by cluse do not use then takes only 4 seconds
can someone explain to me why?|||On Tue, 26 Apr 2005 05:36:03 -0700, mPiccoli wrote:
>my indices:
>PK AktionenPlanID
>IX AdressID
>IX1 MassnahmeID,AktionID
>IX2 LandID,AusgefuehrtAm
>IX3 Wiedervorlage, GeplanteAusfuerhung
>the table contains 1.500.000 data records
>if I order by cluse do not use then takes only 4 seconds
>can someone explain to me why?
Hi mPiccoli,
If you leave out the ORDER BY, then SQL Server can return the rows in any
way it sees fit. With the ORDER BY, it has to do extra work.
If you comare execution plans, I guess you'll see one of the following
scenarios:
1. With ORDER BY, the index IX3 is used; without ORDER BY a different
index is used. Apparently, the other index can find the rows you need more
quickly; to satisfy the ORDER BY, the optimizer decides to use the index
that is already in the required order, even if it means that the rest of
the query takes some more work.
2. With ORDER BY, an extra sorting step is added, but the execution plan
doesn't change otherwise. Apparently, the optimizer estimates that getting
the rows in the wrong order first, then reordering them is better than
using a more expensive method to retrieve the rows.
Since I know nothing about your table structure or data, and the query
itself is quite hard to read as well (please do take Bretana's advice to
heart!) this is about all I can say. If you need more advice, you'll have
to provide more information. Check out www.aspfaq.com/5006.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)
Friday, February 24, 2012
HELP ME TO OPTIMIZE QUERY
Hello friends,
I'm facing performance related problem while running following query on SQL Server 2000.
This query is basically used to find last location of each unit that are passed. Here I am passing data like "'26','27','28','29','30','31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50'" in @.Units variable. But it takes too much time and I don't get output. Table is having around5 Million records.
Query:
SELECT Alias, tblUnit.UnitID, Location, Latitude, Longitude,Convert(varchar(10),UnitHistoryDate,1) + ' ' + substring(Convert(varchar(40),UnitHistoryDate,109),12,9) + ' ' + substring(Convert(varchar(40),UnitHistoryDate,109),25,2) as [Date/Time], unithistoryid
FROM tblUnit INNER JOIN
tblUnitHistory ON tblUnit.UnitID = tblUnitHistory.UnitID
WHERE tblUnitHistory.UnitHistoryDate
IN (SELECT MAX(UnitHistoryDate) FROM tblUnitHistory WHERE tblUnitHistory.UnitID in (' + @.Units + ') GROUP BY tblUnitHistory.UnitID)
AND tblUnit.UnitID in (' + @.Units + ')
ORDER BY tblUnit.UnitID
Table Structure:
UnitHistoryID int Primary Key
UnitID int
Location varchar(200)
Latitude decimal 9
Longitude decimal 9
Speed decimal5
BatteryVoltage decimal5
ReasonCode int
DistanceFromLastLocation decimal9
UnitHistoryDate datetime
Indexes:
1. Clustered Index on ColumnUnitID
2. Non-clustered Index on ColumnUnitHistoryDate
3. Non-clustered Index on ColumnUnitHistoryID
Please help me to write optimize query and suggest me the steps to require making this query faster. Any help is appreciated. Thanks in advance.
Regards,
Sandeep
try to limit processed records by:
'SELECT Alias, tblUnit.UnitID, Location, Latitude, Longitude,Convert(varchar(10),UnitHistoryDate,1) + ' ' + substring(Convert(varchar(40),UnitHistoryDate,109),12,9) + ' ' + substring(Convert(varchar(40),UnitHistoryDate,109),25,2) as [Date/Time], unithistoryid
FROM (SELECT * FROM tblUnit
where
AND tblUnit.UnitID in (' + @.Units + '))tblUnit
INNER JOIN
(SELECT tblUnitHistory.UnitID , MAX(UnitHistoryDate) UnitHistoryDate FROM (SELECT * FROM tblUnitHistory WHERE tblUnitHistory.UnitID in (' + @.Units + ')) tblUnitHistory GROUP BY tblUnitHistory.UnitID) tblUnitHistory
ON tblUnit.UnitID = tblUnitHistory.UnitID
ORDER BY tblUnit.UnitID'
I hope that it will work, The Idea is to limit number of records used in join to minimum.
Sunday, February 19, 2012
Help me optimize a daily events query
We keep a database of events that are classified by severity. I've got a good, efficient query that gives me the grand total for these events. And I have a bad slow query that breaks down the information into daily totals.
My fast query will count the totals for 3213586 events in 4 seconds. The slow daily query takes about 60 seconds for each day.
Please help optimize my slow query!
Database schema is:
- The database view eventView that's used by my queries:
SELECT DATEADD([second], [time] + 60 * offset, '01/01/70') AS timeStr, msec, host, process, dbName, ptName AS point, description, rtuName AS rtu, groupName, message, type, sevInt, [time]
FROM dbo.summary
- Fast totals query:
select
(select count(*)
from [event].[dbo].[eventView]) as Events,
(select count(*)
from [event].[dbo].[eventView]
where type = 'alarm' and sevInt = 1)as Low,
(select count(*)
from [event].[dbo].[eventView]
where type = 'alarm' and sevInt = 2)as Medium,
(select count(*)
from [event].[dbo].[eventView]
where type = 'alarm' and sevInt = 3) as High,
(select count(*)
from [event].[dbo].[eventView]
where type = 'alarm' and sevInt = 4) as Low1,
(select count(*)
from [event].[dbo].[eventView]
where type = 'alarm' and sevInt = 6) as Medium1,
(select count(*)
from [event].[dbo].[eventView]
where type = 'alarm' and sevInt = 8) as High1,
(select count(*)
from [event].[dbo].[eventView]
where type = 'alarm' and sevInt = 9) as High2
- Slow daily query:
declare @.myDay datetime
declare @.begDay datetime
declare @.myTable
table( myDay datetime,
Events int,
Low int, Medium int,
High int, Low1 int, Medium1 int,
High1 int, High2 int )
select @.myDay = getdate()
select @.begDay = convert(datetime, '10/01/2006')
while @.begDay <= @.myDay
begin
insert into @.myTable (myDay) values (convert(varchar(20), @.begDay, 101))
update @.myTable set Events =
(select count(*) as Events
from [event].[dbo].[eventView]
where
convert(varchar, timeStr, 101) = convert(varchar(20), @.begDay, 101))
where myDay = convert(varchar(20), @.begDay, 101)
update @.myTable set Low =
(select count(*) as Low
from [event].[dbo].[eventView]
where type = 'alarm' and sevInt = 1
and convert(varchar, timeStr, 101) = convert(varchar(20), @.begDay, 101))
where myDay = convert(varchar(20), @.begDay, 101)
update @.myTable set Medium =
(select count(*) as Medium
from [event].[dbo].[eventView]
where type = 'alarm' and sevInt = 2
and convert(varchar, timeStr, 101) = convert(varchar(20), @.begDay, 101))
where myDay = convert(varchar(20), @.begDay, 101)
update @.myTable set High =
(select count(*) as High
from [event].[dbo].[eventView]
where type = 'alarm' and sevInt = 3
and convert(varchar, timeStr, 101) = convert(varchar(20), @.begDay, 101))
where myDay = convert(varchar(20), @.begDay, 101)
update @.myTable set Low1 =
(select count(*) as Low1
from [event].[dbo].[eventView]
where type = 'alarm' and sevInt = 4
and convert(varchar, timeStr, 101) = convert(varchar(20), @.begDay, 101))
where myDay = convert(varchar(20), @.begDay, 101)
update @.myTable set Medium1 =
(select count(*) as Medium1
from [event].[dbo].[eventView]
where type = 'alarm' and sevInt = 6
and convert(varchar, timeStr, 101) = convert(varchar(20), @.begDay, 101))
where myDay = convert(varchar(20), @.begDay, 101)
update @.myTable set High1 =
(select count(*) as High1
from [event].[dbo].[eventView]
where type = 'alarm' and sevInt = 8
and convert(varchar, timeStr, 101) = convert(varchar(20), @.begDay, 101))
where myDay = convert(varchar(20), @.begDay, 101)
update @.myTable set High2 =
(select count(*) as High2
from [event].[dbo].[eventView]
where type = 'alarm' and sevInt = 9
and convert(varchar, timeStr, 101) = convert(varchar(20), @.begDay, 101))
where myDay = convert(varchar(20), @.begDay, 101)
select @.begDay = dateadd(day,1,@.begDay)
end
select * from @.myTable
Try this is a replacement for your fast query.
select Count(*) As Events,
Sum(Case When SevInt = 1 Then 1 Else 0 End) As Low,
Sum(Case When SevInt = 2 Then 1 Else 0 End) As Medium,
Sum(Case When SevInt = 3 Then 1 Else 0 End) As High,
Sum(Case When SevInt = 4 Then 1 Else 0 End) As Low1,
Sum(Case When SevInt = 6 Then 1 Else 0 End) As Medium1,
Sum(Case When SevInt = 8 Then 1 Else 0 End) As High1,
Sum(Case When SevInt = 9 Then 1 Else 0 End) As High2,
From [event].[dbo].[eventView]
Where Type = 'alarm'
It should return the same results, but I bet it's at least 10 times faster.
I'm looking at your slow query now. I would bet there it can be improved a lot. One thing I see is that you are doing a lot of converts, which appear to be unnecessary. For example... where myDay = convert(varchar(20), @.begDay, 101) myDay is a date time field (with time removed/set to 0). @.begDay is also a DateTime variable with time removed. Because of this, you should be able to remove the convert from your where clauses.
|||I'm not sure, but this may be a suitable replacement for the slow query. Please double and triple check the results before putting this in to production.
select DateAdd(Day, DateDiff(Day, 0, timeStr), 0) As MyDate,
Count(*) As Events,
Sum(Case When SevInt = 1 Then 1 Else 0 End) As Low,
Sum(Case When SevInt = 2 Then 1 Else 0 End) As Medium,
Sum(Case When SevInt = 3 Then 1 Else 0 End) As High,
Sum(Case When SevInt = 4 Then 1 Else 0 End) As Low1,
Sum(Case When SevInt = 6 Then 1 Else 0 End) As Medium1,
Sum(Case When SevInt = 8 Then 1 Else 0 End) As High1,
Sum(Case When SevInt = 9 Then 1 Else 0 End) As High2,
From [event].[dbo].[eventView]
Where Type = 'alarm'
Group By DateAdd(Day, DateDiff(Day, 0, timeStr), 0)
Order By DateAdd(Day, DateDiff(Day, 0, timeStr), 0)
I would be interested to know if this works for you, and also what the difference in time is between your query and the one I posted here.
|||Shazam! This worked like a charm - I'm stunned really.
Processed the 232 day's worth of data in my system in 6 seconds.
That other one takes about 60 seconds / day's data - so roughly 4 HOURS to do that same job.
I know I'm just a hacker when it comes to doing this stuff. Thanks a LOT for the help. This makes sense to me!
|||The events are counted using a different criteria - they should be calculated just as a count of all the daily messages, not restricted by the where clause.
select count (*) from [event].[dbo].[eventview] as Events
With the code you are suggesting Events are counting all the messages that are of Type = 'alarm'. How to specify a different critieria within that same query?
|||Are you saying this is missing?
and convert(varchar, timeStr, 101) = convert(varchar(20), @.begDay, 101))
Why not add it to mastros' query and then throw that into a stored proc? Just like you started with, but with mastros' query.
|||Instead of "Count(*) As Events", use a sub-query like:
select DateAdd(Day, DateDiff(Day, 0, timeStr), 0) As MyDate,
(select count (*) from [event].[dbo].[eventview]) As Events,
Sum(Case When SevInt = 1 Then 1 Else 0 End) As Low,
Sum(Case When SevInt = 2 Then 1 Else 0 End) As Medium,
Sum(Case When SevInt = 3 Then 1 Else 0 End) As High,
Sum(Case When SevInt = 4 Then 1 Else 0 End) As Low1,
Sum(Case When SevInt = 6 Then 1 Else 0 End) As Medium1,
Sum(Case When SevInt = 8 Then 1 Else 0 End) As High1,
Sum(Case When SevInt = 9 Then 1 Else 0 End) As High2,
From [event].[dbo].[eventView]
Where Type = 'alarm'
Group By DateAdd(Day, DateDiff(Day, 0, timeStr), 0)
Order By DateAdd(Day, DateDiff(Day, 0, timeStr), 0)
I've been struggling with this one for weeks.
What's a good way to learn more about SQL?|||Pick up a SQL book, take a class, and/or hang out on this forum!
Really, though, the best way to learn SQL is to keep working with your data, day in and day out.|||
Making that change (counting events as a sub query) gives me the total of all events.
Sorry - I need a daily total and I'm not clear how to couple that back into the rest of the query.
|||Instead of putting type='alarm' in the where clause, put it in the case statements, like this...
select DateAdd(Day, DateDiff(Day, 0, timeStr), 0) As MyDate,
count (*) As Events,
Sum(Case When SevInt = 1 And Type='alarm' Then 1 Else 0 End) As Low,
Sum(Case When SevInt = 2 And Type='alarm' Then 1 Else 0 End) As Medium,
Sum(Case When SevInt = 3 And Type='alarm' Then 1 Else 0 End) As High,
Sum(Case When SevInt = 4 And Type='alarm' Then 1 Else 0 End) As Low1,
Sum(Case When SevInt = 6 And Type='alarm' Then 1 Else 0 End) As Medium1,
Sum(Case When SevInt = 8 And Type='alarm' Then 1 Else 0 End) As High1,
Sum(Case When SevInt = 9 And Type='alarm' Then 1 Else 0 End) As High2,
From [event].[dbo].[eventView]
Group By DateAdd(Day, DateDiff(Day, 0, timeStr), 0)
Order By DateAdd(Day, DateDiff(Day, 0, timeStr), 0)
Your performance may drop a little because you are processing more rows, but some times that is necessary to get the correct data.
|||Yes it is a tad slower - 8 seconds to query the database versus 6 in the earlier one...|||Do you get the correct results?|||It gets the job done, thanks.
I still don't quite understand this "add another query with different criteria" thing. So I'm going to work on this some more until I can figure out how to get an additional daily value for
select count(*) from [event].[dbo].[eventView] where [message] not like '%NORMAL state%' and [message] not like '%restored - normal%' and [message] not like '%communication%restored%' and [message] not like '%PLM - NORMAL%' and [type] = 'alarm' as Alarms