Search text in fields in every table of a MySQL database
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Searching for a text value across every table in a MySQL database is possible, but MySQL does not provide one built-in command that safely scans every text column everywhere. The usual solution is to inspect INFORMATION_SCHEMA, generate per-table search queries, and run them deliberately. This is a useful debugging and data-audit technique, but it is also expensive, so you should treat it as an investigation tool rather than an everyday query pattern.
Start by Finding Candidate Text Columns
The first step is identifying which columns are worth searching. Usually that means text-like types such as char, varchar, text, mediumtext, and longtext.
This query gives you the searchable surface area of the database. Restricting the search to text-like columns avoids meaningless LIKE checks on numeric or date columns.
Generate Search SQL Dynamically
Once you know the columns, you can build per-table WHERE clauses. A simple pattern is grouping text columns per table and concatenating LIKE predicates.
This does not execute the queries automatically. It generates them so you can inspect or run them deliberately.
That manual review step matters because broad searches can be expensive and may need further filtering.
Use Application Code When You Need Better Control
For operational searches, a small script is often clearer than pushing everything into one stored SQL statement. A script can:
- paginate through tables
- log matches cleanly
- parameterize the search safely
- skip large or irrelevant tables
This keeps query generation explicit and avoids some of the awkwardness of pure SQL meta-programming.
Expect Performance Costs
A database-wide text scan is naturally expensive. LIKE '%text%' cannot use ordinary indexes effectively because the pattern starts with a wildcard. On large tables, this often becomes a full scan.
That means you should:
- run it sparingly
- narrow by schema or table when possible
- use
LIMITduring exploration - avoid doing it on production hot paths unless absolutely necessary
If this kind of search is a regular application requirement, the answer is usually not better LIKE generation. The answer is proper full-text indexing or a dedicated search system.
Distinguish Investigation from Product Features
Searching all fields in every table is reasonable for troubleshooting, migration audits, or forensic cleanup. It is usually a bad design for user-facing application search. Production search features should rely on defined searchable columns and indexing strategy, not dynamic scans of the entire relational schema.
That distinction keeps database debugging tools from silently becoming application architecture.
Common Pitfalls
- Running
LIKE '%text%'across every column without restricting the scan to text-like types. - Building dynamic SQL unsafely and interpolating raw search text directly into queries.
- Forgetting that wildcard-leading
LIKEsearches usually force full scans. - Treating a cross-database investigation query as if it were a normal application search pattern.
- Executing broad scans in production without table limits, scheduling, or operational awareness.
Summary
- MySQL does not have one built-in command to search every field in every table.
- Use
INFORMATION_SCHEMA.COLUMNSto discover text-like columns first. - Generate per-table search queries dynamically and run them deliberately.
- Prefer a small script when you need safer parameterization and cleaner reporting.
- For recurring search requirements, use indexing or a search-specific design instead of database-wide
LIKEscans.
Related reading
- Search text in stored procedure in SQL Server
- Secondary-only nodes in mongodb Replica set
- Select 2000 most recent log entries in cassandra table using CQL Latest version
- Select all columns except one in MySQL?
- Select columns across different databases
- Select data from show tables MySQL query
- SELECT FROM X WHERE id IN ... with Dapper ORM
- Select last N rows from MySQL

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.