title: TOP clause applied in @NamedQuery
body: <p>I have named query
  <code>@NamedQuery(name="CustomerMarket.findByMarketId", query="SELECT DISTINCT c.marketid FROM CustomerMarket c WHERE c.marketid LIKE :mask")</code>
  in my class CustomerMarket definition.</p>
  
  <p>I want to take first 1_000 of 350_000 results so I applied setMaxResult:</p>
  
  <pre><code>Query market = session.getNamedQuery("CustomerMarket.findByMarketId");
  market.setString("mask", getMask() + "%");
  market.setMaxResults(1000);
  List&lt;Object&gt; result = market.list()
  </code></pre>
  
  <p>After performing this query I checked server.log and found this </p>
  
  <pre><code>select distinct customerma0_.marketid as col_0_0_ 
  from   CUSTOMER_MARKET_S customerma0_ 
  where  customerma0_.marketid like ?
  </code></pre>
  
  <p>I have got correct data, first 1_000 records, but I want to know if there is TOP restriction applied and where.</p>
  
  <p>How is this TOP clause handled?</p>
  
  <ol>
  <li>TOP is applied into callable statement which is used by Hibernate and this is not displayed in log</li>
  <li>TOP is applied after receiving result from DB and filters data on Java side</li>
  <li>Or anything else</li>
  </ol>
  
  <p>I have set up persistance.xml and log4j.properties for displaying almost everything, but I think that there is no problem, since I have got SQL script.</p>
  
  <p><strong>EDIT</strong>:
  I did some measurement of the performance in both cases (with and without setMaxResults) and got following times:</p>
  
  <pre><code>long time = System.currentTimeMillis();
  market.list();
  LOGGER.info("time: " + (System.currentTimeMillis() - time) + "ms");
  </code></pre>
  
  <p>In the first case without TOP I had average time consumption 1400ms per select.
  In the second case with TOP implemented I had average time consumption around 150ms.</p>
  
  <p>It was also said to me, that this TOP implementation may be dependent on used Hibernate dialect (we are using Sybase). And the select which appears in log is exactly the same which is send to DB. 
  But in this case is it normal that converting 350k results from resultset (or how is result stored in Hibernate session) into structure List without any metadata takes such long time?
  I can imagine that internally it is converted something like that:</p>
  
  <pre><code>**SQL generating process**
  **SQL setting parameters**
  ResultSet rs = executeQuery();
  List&lt;Object[]&gt; result = new ArrayList&lt;Object[]&gt;(rs.getFetchSize());
  int rowSize = rs.getMetaData().getColumnCount()
  while (rs.next) {
    Object[] row = new Object[rowSize];
    for (int i = 0; i &lt; rowSize; i++) {
      row[i] = rs.getObject(i);
    }
  }
  </code></pre>
  
  <p>This does not seems to be so complicated, so why is there this time difference. Probably I am naive because Hibernate is complex framework with lot of features. :-D</p>