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

  • SEARCH
  • Home
  • 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 6614029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:17:22+00:00 2026-05-25T20:17:22+00:00

shopkeeper table has following fields: id (bigint),amount (numeric(19,2)),createddate (timestamp) Let’s say, I have the

  • 0

shopkeeper table has following fields:

id (bigint),amount (numeric(19,2)),createddate (timestamp)

Let’s say, I have the above table. I want to get the records for yesterday and
generate a report by having the amount printed to cents.

One way of doing is to perform calculations in my java application and execute a simple query

Date previousDate ;// $1 calculate in application

Date todayDate;// $2 calculate in application

select amount where createddate between $1 and $2 

and then loop through the records and convert amount to cents in my java application and generate the report

Another way is like performing calculations in sql query itself:

select cast(amount * 100 as int) as "Cents"
from shopkeeper  where createddate  between date_trunc('day', now()) - interval '1 day'  and  date_trunc('day', now())

and then loop through the records and generate the report

In one way , all my processing is done in java application and a simple query is fired.
In other case all the conversions and calculations is done in Sql query.

The above use case is just an example, in a real scenario a table can have many columns that require processing of the similar kind.

Can you please tell me which approach is better in terms of performance and other aspects and why?

  • 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-05-25T20:17:23+00:00Added an answer on May 25, 2026 at 8:17 pm

    It depends on a lot of factors – but most crucially:

    • complexity of calculations (prefer doing complex crunching on an app-server, since that scales out; rather than a db server, which scales up)
    • volume of data (if you need to access/aggregate a lot of data, doing it at the db server will save bandwidth, and disk io if the aggregates can be done inside indexes)
    • convenience (sql is not the best language for complex work – especially not great for procedural work, but very good for set-based work; lousy error-handling, though)

    As always, if you do bring the data back to the app-server, minimising the columns and rows will be to your advantage. Making sure the query is tuned and appropriately indexed will help either scenario.

    Re your note:

    and then loop through the records

    Looping through records is almost always the wrong thing to do in sql – writing a set-based operation is preferred.

    As a general rule, I prefer to keep the database’s job to a minimum “store this data, fetch this data” – however, there are always examples of scenarios where an elegant query at the server can save a lot of bandwidth.

    Also consider: if this is computationally expensive, can it be cached somewhere?

    If you want an accurate “which is better”; code it both ways and compare it (noting that a first draft of either is likely not 100% tuned). But factor in typical usage to that: if, in reality, it is being called 5 times (separately) at once, then simulate that: don’t compare just a single “1 of these vs 1 of those”.

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

Sidebar

Related Questions

I have used the following code to connect between java and mysql, but it
I have the below JQuery. Nothing happens and I have no idea how to
I have the below query which does not work. What am I doing wrong?
I have the below code to loop through and run an external file. I
I have the below code to open a CSV file, but for some reason
I need to loop through the items as an array within the SimpleXMLElement Object

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.