Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7793635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:33:02+00:00 2026-06-01T22:33:02+00:00

I am trying to write a database independant application with JDBC. I now need

  • 0

I am trying to write a database independant application with JDBC. I now need a way to fetch the top N entries out of some table. I saw there is a setMaxRows method in JDBC, but I don’t feel comfortable using it, because I am scared the database will push out all results, and only the JDBC driver will reduce the result. If I need the top 5 results in a table with a billion rows this will break my neck (the table has an usable index).

Writing special SQL-statements for every kind of database isn’t very nice, but will let the database do clever query planning and stop fetching more results than necessary.

Can I rely on setMaxRows to tell the database to not work to much?

I guess in the worst case I can’t rely on this working in the hoped way. I’m mostly interested in Postgres 9.1 and Oracle 11.2, so if someone has experience with these databases, please step forward.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T22:33:03+00:00Added an answer on June 1, 2026 at 10:33 pm

    will let the database do clever query planning and stop fetching more
    results than necessary.

    If you use

    PostgreSQL:

    SELECT * FROM tbl ORDER BY col1 LIMIT 10; -- slow without index
    

    Or:

    SELECT * FROM tbl LIMIT 10;               -- fast even without index
    

    Oracle:

    SELECT *
    FROM   (SELECT * FROM tbl ORDER BY col1 DESC)
    WHERE  ROWNUM < 10;
    

    .. then only 10 rows will be returned. But if you sort your rows before picking top 10, all basically qualifying rows will be read before they can be sorted.

    Matching indexes can prevent this overhead!


    If you are unsure, what JDBC actually send to the database server, run a test and have the database engine log the statements received. In PostgreSQL you can set in postgresql.conf:

    log_statement = all
    

    (and reload) to log all statements sent to the server. Be sure to reset that setting after the test or your log files may grow huge.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write some variable data back to a database table when
I am trying to write to a table in my sqlite database in android
Trying to write out syslog entries containing strings but they don't register. // person.name
I am trying to write some code to test a database model. Both the
I'm trying to write some loggin failure info in database from a custom authentication
I am trying to write three variables into a database table. The code I
In trying to write a simple C# ADO.NET application to connect to my database
I'm trying to write a simple, thin and efficient database abstraction layer on top
I am trying to write data to a table in the database. I finally
I'm trying to write a application that sends user's geolocation to MYSQL database every

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.