SQL - view very slow or doesnt return any results

时间:2016-02-12 20:36:12

标签: sql oracle greatest-n-per-group

I am new to SQL and struggling with very slow view, it should return newest event for specific person. This view doesnt use PERSON entity (but it could). The relation Person->Event is oneToMany.

Structure

 
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Case</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
</head>
<body>
<header>
<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-1">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>                        
      </button>
       <a href="#"><img src="logo.png"></a>
    </div>
    <div class="collapse navbar-collapse" id="navbar-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#">Why us</a></li>
        <li><a href="#">About us</a></li>
        <li><a href="#">Pricing</a></li>
        <li><a href="#">FAQ</a></li>
        <li><a href="#">Blog</a></li>
        <li><a href="#">Reviews</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </div>
  </div>
</nav>
</header>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</body>
</html>

Below is very slow view, which works too slow (indexes on PERSON_ID and ID are created but it is still very slow)

PERSON: ID NAME EVENT: PERSON_ID EVENT_DESC EVENT_TYPE IDX

View to be changed (too slow)

select * from V_CURRENT_EVENT_FOR_PERSON where person_id = 123456

I tried to change it to:

create or replace force view "V_CURRENT_EVENT_FOR_PERSON" ("PERSON_ID", 
    "EVENT_DESC", "EVENT_TYPE") as 
select * from (SELECT EVENT.PERSON_ID            AS PERSON_ID,

    EVENT.EVENT_DESC AS EVENT_DESC,
    EVENT.EVENT_TYPE     AS EVENT_TYPE

  FROM PERSON_EVENT_DATA EVENT
  WHERE EVENT.ID =
    (SELECT EVENT2.ID
    FROM PERSON_EVENT_DATA EVENT2
    WHERE EVENT2.PERSON_ID = EVENT.PERSON_ID
    GROUP BY EVENT2.PERSON_ID,
      EVENT2.IDX,
      EVENT2.ID
    HAVING EVENT2.IDX =
      (SELECT MAX(EVENT3.IDX)
      FROM PERSON_EVENT_DATA EVENT3
      WHERE EVENT3.PERSON_ID = EVENT2.PERSON_ID
      )
    ))

But here create or replace force view "V_CURRENT_EVENT_FOR_PERSON" ("PERSON_ID", "EVENT_DESC", "EVENT_TYPE") as select * from (SELECT EVENT.PERSON_ID AS PERSON_ID, EVENT.EVENT_DESC AS EVENT_DESC, EVENT.EVENT_TYPE AS EVENT_TYPE FROM PERSON_EVENT_DATA EVENT order by EVENT.idx desc ) where rownum = 1 never retures any results because of the select * from V_CURRENT_EVENT_FOR_PERSON where person_id = 123456

1 个答案:

答案 0 :(得分:2)

If I understood your question correctly, the following should do what you want:

.popover("show")