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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:55:34+00:00 2026-05-23T00:55:34+00:00

I was prototyping an SQL query quickly, and instead of doing it the correct

  • 0

I was prototyping an SQL query quickly, and instead of doing it the correct way, I just decided to slam through it with a bunch of string concats, the entire time thinking that this would be extremely slow, but it didn’t matter because I was just testing the query. To my suprise, Java says that this code take 0 ms to complete? Doesn’t it take more time do this with + rather than StringBuilder or similar?

long t = System.currentTimeMillis();
String load = "";
for (String s : loadFields)
    load += s + ", ";

String sql = "SELECT ";
sql += load + "sum(relevance) AS 'score' " +
        "FROM ( ";

for (int i = 0; i < searchFields.length; i++) {
    sql += "SELECT ";
    sql += load;
    sql += rels[i] + " AS relevance FROM articles WHERE " +
            searchFields[i];

    sql += " LIKE '%" + terms[0] + "%' ";
    for (int z = 1; z < terms.length; z++)
        sql += "AND " + searchFields[i] + " LIKE '%" + terms[z] + "%' ";

    if (i != searchFields.length - 1) sql += " UNION ALL ";
}

sql += ") results GROUP BY " + load.substring(0, load.length() - 2) + " ";
sql += "ORDER BY score desc, date desc";
System.out.println("Build Time: " + (System.currentTimeMillis() - t) + " ms");

Yes, this is very ugly, but the point is not to iterpret the SQL, but to tell me why this is so fast.

Build Time: 0 ms

Edit: I ran the test 10000 times with 20 terms, and it took about 10 seconds, so about 1/10 of a millisecond. Now that I think about it, it is pretty obvious that this isn’t that much to computation unless I start getting really long strings.

  • 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-23T00:55:35+00:00Added an answer on May 23, 2026 at 12:55 am

    You’re only doing 29 concatenations – I should hope that takes less than a millisecond.

    If you want to test this code against a StringBuilder implementation, you should iterate it 10,000 times or so (and do proper JVM warm-up).

    Benchmark

    I was curious to see what the exact difference is in this case, so I converted your code to use .concat() and StringBuilder and benchmarked with 10,000 iterations (2,000 warmup), with 5 fields and 20 terms, all of them randomly generated 32 char strings.

    Results (in milliseconds):

       plus: 19656 (0.5/ms)
     concat: 5656  (1.77/ms)
    builder: 578   (17.3/ms)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was just prototyping a new system for deferring certain operations until out of
I am prototyping an app using ASP.NET MVC and SQL Compact Edition on VS2008.
Is there a way to generate the DBML file from entity classes instead from
I want rapid prototyping: Quickly define screens, links, buttons etc. I want to easily
When prototyping initial GUI functionality with a customer is it better to use a
I am prototyping some C# 3 collection filters and came across this. I have
We are looking for a prototyping tool to allow our business analysts (and myself)
I love scaffolding and it extremely helpful for prototyping. But Should we use scaffolding
I'm prototyping my first MVC application, it's a simple forum. I've done part of
Is it possible to get a LINQ to SQL DataContext to run completely in-memory?

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.