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 7932341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:59:16+00:00 2026-06-03T20:59:16+00:00

I am fetching records from MySQL database using Java (JDBC). I have tables –

  • 0

I am fetching records from MySQL database using Java (JDBC). I have tables –
Stop_Times with 1.5 Million records and
Stops with 1 lac records.

I am using following code

ResultSet rs = stm.executeQuery("select distinct(stop_id) from Stop_Times force index (idx_stop_times) where agency_id = '" + agency_id + "' and route_type = " + route_type + " order by stop_id");

while(rs.next())
{
  stop_id.add(rs.getString("stop_id"));               
}

JSONArray jsonResult = new JSONArray();

String sql = "select * from Stops force index (idx_Stops) where stop_id = ? and agency_id = ? and location_type = 0 order by stop_name";

                  PreparedStatement pstm = con.prepareStatement(sql);

                  int rid = 0;

                  for(int r = 0; r < stop_id.size(); r++)
                  {
                      pstm.setString(1, stop_id.get(r).toString());
                      pstm.setString(2, agency_id);
                      rs = pstm.executeQuery();

                      if(rs.next())
                      {
                          JSONObject jsonStop = new JSONObject();
                          jsonStop.put("str_station_id", rs.getString("stop_id"));
                          jsonStop.put("str_station_name", rs.getString("stop_name") + "_" + rs.getString("stop_id"));
                          jsonStop.put("str_station_code", rs.getString("stop_code"));
                          jsonStop.put("str_station_desc", rs.getString("stop_desc"));
                          jsonStop.put("str_station_lat", rs.getDouble("stop_lat"));
                          jsonStop.put("str_station_lon", rs.getDouble("stop_lon"));
                          jsonStop.put("str_station_url", rs.getString("stop_url"));
                          jsonStop.put("str_location_type", rs.getString("location_type"));
                          jsonStop.put("str_zone_id", rs.getString("zone_id"));

                          jsonResult.put((rid++), jsonStop);
                      }                               
                  }

The first query returns 6871 records. But it is taking too much time – on server side it is taking 8-10 seconds and at client side 40-45 seconds.

I want to reduce these times as for server side 300-500 milliseconds and at client side around 10 sec.
Please can anybody help me for how to to this?

  • 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-03T20:59:17+00:00Added an answer on June 3, 2026 at 8:59 pm

    Your strategy is to use a first query to get IDs, and then loop over these IDs and execute another query for each of the IDs found by the first query. You’re in fact doing a “manual” join instead of letting the database do it for you. You could rewrite everything in a single query:

    select * from Stops stops
    inner join Stop_Times stopTimes on stopTimes.stop_id = stops.stop_id
    where stops.stop_id = ? 
      and stops.agency_id = ? 
      and stops.location_type = 0 
      and stopTimes.agency_id = ? 
      and stopTimes.route_type = ?
    order by stops.stop_name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form i am fetching many records from database that are rendering
I have an problem in fetching records from database with php. I have 31700
I have a Datatable which is fetching ~5,50,000 records from database(SQLite). When fetching it
I am fetching all records from a db table into a page using; SELECT
I am fetching records from gae model using cursor() and with_cursor() logic as used
I have a table and from that I am fetching records somewhere around 250,000
I am using MySQL and Java to SELECT about 50000 records. The strange thing
My problem is that NHibernate gets exponentially slow when fetching records from the database.
I have table from which I am fetching records for two time interval as
Let say i have 100k records in table, after fetching that records from table

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.