max date select stmt problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matik

    max date select stmt problem

    Hello Everybody,

    I have a problem, with select stmt:

    SELECT TOP 15 *
    FROM oaVIEW_MainData AS TOP_VIEW,
    oaLanguageData_ TAB AS RwQualifierJoin with (nolock)
    WHERE (c_dateTime>='2 007.01.10 00:00:00' AND c_dateTime<='20 07.01.10
    23:59:59')
    AND RwQualifierJoin .text_id = c_cfgRegPoint
    AND (((RwQualifierJ oin.local1 LIKE N'Position of any bubu')))
    AND TOP_VIEW.c_date Time=(SELECT MAX(SUB_VIEW.c_ dateTime)
    FROM oaVIEW_MainData AS SUB_VIEW,oaLang uageData_TAB AS
    RwQualifierJoin 1 with (nolock)
    WHERE (c_dateTime>='2 007.01.10 00:00:00' AND c_dateTime<='20 07.01.10
    23:59:59')
    AND RwQualifierJoin 1.text_id = c_cfgRegPoint
    AND (((RwQualifierJ oin1.local1 LIKE N'Position of any bubu')))
    AND TOP_VIEW.c_dsmI dent=SUB_VIEW.c _dsmIdent)
    order by c_dateTime desc


    Please consider:
    - top doesn't metter, if I will use one or 10000 result is always the
    same.
    - oaVIEW_MainData , is a view on major big table, holding lot of records
    joinden with small table containing configuration data, over left outer
    join; both tables are with nolock option,
    - quersy supose to return last record from major table/view, in given
    time, additionaly, with other where conditions (like in this case with
    text),
    - on major table, are indexes which one is on id field (not used in
    this query at all), which is a pk clustered, and other is on dateEvt
    (c_dateTime) which is a desc index with fill level 90%
    - table has also other indexes, on three different fields, one of
    theses is dsmIdent,

    Now, if I'm using max(id) works very fast, and ok for me, but the
    problem is, I should not use id, because might be, that the records
    will be written in the table with random order, so the only one saying
    which is newest, will be dateEvt.

    Using dateEvt as max(), dramaticly slows query, so I'm acctualy unable
    to get result. What is much more funny, server is totaly busy with this
    query, and it's procesor jumps on 100%.

    Now, because the query is builded dynamicly, by a user selections,
    that's why we decided on such a parser ... problem is, it is not
    working :(

    Can I change index on dateEvt somehow, to sped this up?
    Maybe construct query somehow different, to get this over max() date?

    Please help

    Matik

  • Erland Sommarskog

    #2
    Re: max date select stmt problem

    Matik (marzec@sauron. xo.pl) writes:
    - oaVIEW_MainData , is a view on major big table, holding lot of records
    joinden with small table containing configuration data, over left outer
    join; both tables are with nolock option,
    NOLOCK in a view? That's about criminal in my opinion.
    - on major table, are indexes which one is on id field (not used in
    this query at all), which is a pk clustered, and other is on dateEvt
    (c_dateTime) which is a desc index with fill level 90%
    - table has also other indexes, on three different fields, one of
    theses is dsmIdent,
    >
    Now, if I'm using max(id) works very fast, and ok for me, but the
    problem is, I should not use id, because might be, that the records
    will be written in the table with random order, so the only one saying
    which is newest, will be dateEvt.
    >
    Using dateEvt as max(), dramaticly slows query, so I'm acctualy unable
    to get result. What is much more funny, server is totaly busy with this
    query, and it's procesor jumps on 100%.
    Well, the easy fix would be to make the index on dateEvt() clustered
    rather than the index on id. That may of course have repercussions
    elsewhere.

    The query looks funny to me, as it repeats the entire outer query in
    the subquery. Somehome I feel that that should not be necessary. But
    to say for sure I would need to know the view definition and the
    definition of the underlying tables, including their key and check
    constraints.



    --
    Erland Sommarskog, SQL Server MVP, esquel@sommarsk og.se

    Books Online for SQL Server 2005 at

    Books Online for SQL Server 2000 at

    Comment

    Working...