Documente Academic
Documente Profesional
Documente Cultură
tools
for InterBase and Firebird
databases
IBSurgeon contacts:
www.ib-aid.com
support@ib-aid.com
InterBase/Firebird DBA Tips&Tricks
Contents
How to get statistics from InterBase/Firebird database in right way..................................3
Abstract.....................................................................................................................3
Right time, right place...............................................................................................3
If you does not experience periodical performance problems..................................3
If you experience periodical performance problems.................................................4
What to do with this statistics....................................................................................4
When DBA can't do nothing......................................................................................5
IBAnalyst: More Tips and Tricks.........................................................................................6
1. How to rebuild indices on PRIMARY, FOREIGN or UNIQUE constraints?..........6
2. I used all IBAnalyst recommendations but this won't help to speedup queries....6
4. Record versions for table that is must not be updated ........................................6
5. Blobs can cause table fragmentation....................................................................6
6. VerLen and RecLength relation............................................................................7
7. Why IBAnalyst name some indices as "bad" ?.....................................................7
8. What if "bad" index created by Foreign Key constraint?......................................8
9. Why in Data version percent row there are only 12 megabytes data, but I have
140 megabytes database ?.......................................................................................8
10. How to improve optimizer performance in case of frequent updates ................8
© IBSurgeon, 2005-2009 2
InterBase/Firebird DBA Tips&Tricks
Abstract
This document is devoted to tips and tricks of gathering and analyzing statistics from
InterBase/Firebird databases with or without IBAnalyst.
Yes, your applications can be designed so perfect that they will always work with
transactions and data correctly, not making sweep gaps, lot of active transactions, long
running snapshots and so on. Usually it does not happen. At least because some
developers test their applications running 2-3 simultaneous users at the same time, not
more. Thus, when they set up written applications for 15 and more simultaneous users,
database can behave unpredictably. Of course, multiuser mode can work Ok, because
most of multiuser conflicts can be tested with 2-3 concurrently running applications. But,
next, when more concurrent applications will run, garbage collection problems can came
(at least). And this can be caught if you take statistics at the correct moments.
The most valuable information is transactions load and version accumulation. This can be
seen only if you setup regular statistics saving.
InterBase does not have internal task scheduler, so you are free to use any external, like
standard Task Scheduler (Windows) or cron (Unix).
The best setup is to get hourly transaction statistics. This can be done by running
where
© IBSurgeon, 2005-2009 3
InterBase/Firebird DBA Tips&Tricks
gstat –h db.gdb
What does this mean, you can ask? We'll give example of some system, where
performance problems happened each morning for 20-30 minutes. That was very sufficient
for "morning" applications, and could not last longer.
Database admin was asked correct questions, and here is the picture:
Daily work was divided by sections – analytics works in the morning, than data is inserted
and edited by usual operators, and at the end of the day special procedures started
gathering data, that would be used for analytics next day (at least).
The last work on database at the end of day was lot of updates, and updates of those
tables which analytics used in the morning. So, there were a lot of garbage versions, which
started to be collected by application, running in the morning.
And, the answer to that problem was found simple – to run gfix –sweep at the end of the
day.
Sweep reads all tables in database and tries to collect all garbage versions for committed
and rolled back transactions. After sweeping database became clear nearly it comes after
restore.
© IBSurgeon, 2005-2009 4
InterBase/Firebird DBA Tips&Tricks
© IBSurgeon, 2005-2009 5
InterBase/Firebird DBA Tips&Tricks
2. I used all IBAnalyst recommendations but this won't help to speedup queries.
A: This is separate issue, where IBAnalyst can't help. Here can be 2 causes of problem:
1. Indices have outdated statistics. You can refresh index statistics by command SET
STATISTICS INDEX xxx, or in IBExpert (menu Database, Recompute selectivity of all
indices), or using gidx tool (www.ibase.ru/download/gtools.zip).
2. Queries are hard themselves, or optimizer can't optimize query.
3. In some cases you will see "fragmented tables" right after restore.
Normally Interbase (without parameter -use_all_space) reserves about 25% space on data
pages for future inserts, updates or deletes (to place record versions). But, with any
database page size (1, 2, 4 or 8 k) you will see ~50% fragmentation for tables, that have
small record size (about ~12-20 bytes, for example, table with 2 integer fields have
average record size = 12 bytes).
This is OK, consider this as some magic server number (or behavior).
So, if you have such small record tables, you can
a) ignore "fragmented" warning for that tables
b) lower "fragmented %" to 45%, for example, in IBAnalyst Options dialog.
© IBSurgeon, 2005-2009 6
InterBase/Firebird DBA Tips&Tricks
3. 3. if in case 2 blob does not fit on one data page, pointer page is created to point at
appropriate blob pages.
Case 1 happens depending of stored blob size and database page size. For example, if
you had page size 4K and blobs with average size ~5K they are stored not at data pages,
but at additional blob pages.
But if you backup your database and restore it with 8K page size, blobs will fit at data
page, and they will be stored with records, causing high record fragmentation.
IBAnalyst marks these tables as Pale (Records column) and hint shows estimated records
for that table (based on data pages count) and real average fill value (%).
If your query reads any fields except blobs from that table, natural scan, join or aggregate
will run very slow.
Right now is the only one solution to avoid this - create additional table (linked 1-1 to
original table) and move all blob columns that have average size less than page size to it.
Firebird 2.0 will have option that can prevent storing blobs at data pages at all.
In that case don't try to backup/restore with greater page size! This will cause blobs that
couldn't fit at data pages with current page size, will be placed at data pages during
restore with bigger page size. So, your tables with blobs will be fragmented more than
earlier.
Also it is not recommended to restore with smaller page size, because it can decrease
performance for indices and non-blob tables.
Also you should not try to change blob fields to varchar fields - varchar fields are always
stored as a part of record, so record can have 2 or more fragments (be placed at 2 or more
data pages) if it doesn't fit at data page.
p.s. IBAnalyst can report these tables "by mistake", for example, table had blob fields with
data, but they were dropped from table structure. Unfortunately there is no configurable
option for that warning, because we calculate it exactly from data being reported by server
(statistics).
© IBSurgeon, 2005-2009 7
InterBase/Firebird DBA Tips&Tricks
4. If this index is used in where clause, memory usage will depend on value being
searched (bitmask size). Since record chain can be big (lot of key duplicates), memory
consumption will be also big.
5. If that index is used in "order by", and lot of duplicates mostly in lower key values
(depending on index sort order), there will be lot of index page reads, that will slowdown
query.
That's because IBAnalyst can't ignore such indices existence.
Worst case for index is when it have Uniques column =1, i.e. all values for indexed column
are the same. These indices are listed in "Useless indices" at Summary page.
Of course, for your application such an index may be "good". For example, if records have
"archive" flag in some column, and your application search by index on that column only
for current, not archived data. Thus, its up to you, are we right naming that index "bad", or
not.
9. Why in Data version percent row there are only 12 megabytes data, but I have 140
megabytes database ?
1. IBAnalyst here shows "pure" data volume, without count of other database structures
(indices, metadata...) and page fragmentation.
2. After restore InterBase and Firebird leaves some free space (15-25%) at data pages
to make faster future updates/deletes.
3. There are specific server behavior when it leaves data pages fragmented by ~50%, if
that table record size is low, about 11-22 bytes.
© IBSurgeon, 2005-2009 8
InterBase/Firebird DBA Tips&Tricks
ACTIVE")
The optimizer uses this statistics information to prepare queries. Using statistics values
optimizer can decide that index is "good enough" or "not useful" for retrieving records.
If statistics was not updated during a long time optimizer may produce a bad plan because
existing statistics values does not correspond the actual state of affairs, because table
data can be significantly changed (for example, quantity of records was increased in 5-10
times, or vice versa, all records were deleted).
You can replace the bad automatic query plan that with explicit PLAN for particular query,
but this is not a good approach, because data can be significantly changed after the plan
was developed.
Alternate (and right) way is to refresh statistics periodically by applying SET STATISTICS
statement for all indices. You can schedule to run SQL script to refresh statistics using
ISQL or ready to use tool gidx (Windows only).
If you have some tables with periodically reloaded different records this approach will not
help. Let's consider the example:
• Table A is being loaded with data 4-5 times per day.
• After processing of loaded data all records in table A are being deleted
In this case we can see 2 correct statistics values for indices on table A - when it is loaded
with data, and when it is empty. So, statistics recomputed on loaded table will be useless
when table is empty, and vice versa.
To avoid this you need to recompute statistics for indices on table A only when table is
filled with data. The best is before queries on that table will be run.
Since version 1.91, IBAnalyst shows index statistics difference and allow you to recompute
it at any moment. First you need to look at table record information - is that usual average
record count or not. If yes, you may recompute index selectivity for sure. If not - maybe it
will be better do not touch index statistics, because it can cause optimizer to produce even
worse query plans.
© IBSurgeon, 2005-2009 9