liveBook · Manning

SQL

As explained in Troubleshooting Java, Second Edition

An SQL query is a statement expressed in Structured Query Language that requests or modifies data in a database system.

An SQL query is a statement sent to a database management system to retrieve or modify data. Applications may write queries directly or generate them through persistence frameworks. section 6.0 section 6.3.2

SQL queries become an investigative concern when an application lags, times out, or behaves inefficiently. Profiling reveals which queries execute, how long they take, how often they run, and which application code sent them. section 6.3 section 6.4

A practical query investigation asks:

  • Which endpoint or operation triggers the query? section 6.3.1
  • How many times does the query execute, and does that match the application logic? section 6.3.1
  • Which call stack and framework operation produced it? section 6.3.2
  • Can the captured query be copied into a SQL client for further investigation? section 6.3.2

Finding repeated product lookups

Calling /products returns purchased product names. The application first loads purchases, then calls findProduct() for each purchase. Profiling exposes the executed SQL, its stack trace, and its invocation count; ten repository calls may reveal an inefficient access pattern. With native JDBC, the query can appear directly as SELECT * FROM product WHERE id = ?. With Spring Data JPA and Hibernate, profiling still captures the generated SQL before it reaches the DBMS, while logging can show bound parameters and extracted values. section 6.3.1 section 6.3.2

How Troubleshooting Java, Second Edition covers it

How deep it goes
Explains direct and framework-generated queries, profiler interception, stack traces, execution counts, and logging, but defers query optimization details.
Examples
Uses product and purchase tables, a purchased-products endpoint, native SELECT statements, generated Hibernate SQL, and execution logs.
What it assumes
Assumes basic application and database interaction, while demonstrating how to investigate queries without reading persistence implementation code first.
What you can do after
Diagnose database performance by capturing SQL with a profiler, tracing its caller, counting executions, and examining it in a SQL client.
How it connects
Connects SQL queries with database management systems, JDBC, profilers, JPA, Hibernate, Spring Data, and JPQL.

Also explained in

Compare how 25 books explain SQL →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Troubleshooting Java, Second Edition. The section links point to the parts of the book it draws on.