[go: up one dir, main page]

File: toanalyze.cpp

package info (click to toggle)
tora 1.3.23-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 15,984 kB
  • ctags: 14,460
  • sloc: cpp: 123,554; sh: 16,181; makefile: 966; xml: 69
file content (620 lines) | stat: -rw-r--r-- 20,523 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/*****
*
* TOra - An Oracle Toolkit for DBA's and developers
* Copyright (C) 2003-2005 Quest Software, Inc
* Portions Copyright (C) 2005 Other Contributors
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation;  only version 2 of
* the License is valid for this program.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*
*      As a special exception, you have permission to link this program
*      with the Oracle Client libraries and distribute executables, as long
*      as you follow the requirements of the GNU GPL in regard to all of the
*      software in the executable aside from Oracle client libraries.
*
*      Specifically you are not permitted to link this program with the
*      Qt/UNIX, Qt/Windows or Qt Non Commercial products of TrollTech.
*      And you are not permitted to distribute binaries compiled against
*      these libraries without written consent from Quest Software, Inc.
*      Observe that this does not disallow linking to the Qt Free Edition.
*
*      You may link this product with any GPL'd Qt library such as Qt/Free
*
* All trademarks belong to their respective owners.
*
*****/

#include "utils.h"

#include "toanalyze.h"
#include "toconf.h"
#include "toconnection.h"
#include "tomain.h"
#include "tomemoeditor.h"
#include "toresultcombo.h"
#include "toresultlong.h"
#include "toresultplan.h"
#include "toresultview.h"
#include "tosql.h"
#include "totool.h"
#include "toworksheetstatistic.h"

#include <qcombobox.h>
#include <qlabel.h>
#include <qmenubar.h>
#include <qpopupmenu.h>
#include <qspinbox.h>
#include <qsplitter.h>
#include <qtabwidget.h>
#include <qtimer.h>
#include <qtoolbar.h>
#include <qtoolbutton.h>
#include <qworkspace.h>

#include "toanalyze.moc"

#include "icons/execute.xpm"
#include "icons/refresh.xpm"
#include "icons/sql.xpm"
#include "icons/stop.xpm"
#include "icons/toanalyze.xpm"

class toAnalyzeTool : public toTool
{
    virtual const char **pictureXPM(void)
    {
        return const_cast<const char**>(toanalyze_xpm);
    }
public:
    toAnalyzeTool()
            : toTool(320, "Statistics Manager")
    { }

    virtual void closeWindow(toConnection &connection){};

    virtual const char *menuItem()
    {
        return "Statistics Manager";
    }
    virtual QWidget *toolWindow(QWidget *parent, toConnection &connection)
    {
        return new toAnalyze(parent, connection);
    }
    virtual bool canHandle(toConnection &conn)
    {
        return toIsOracle(conn) || toIsMySQL(conn);
    }
};

static toAnalyzeTool AnalyzeTool;

static toSQL SQLListTablesMySQL("toAnalyze:ListTables",
                                "toad 0,* show table status",
                                "Get table statistics, first three columns and binds must be same",
                                "4.1",
                                "MySQL");
static toSQL SQLListTables("toAnalyze:ListTables",
                           "select 'TABLE' \"Type\",\n"
                           "       owner,\n"
                           "       table_name,\n"
                           "       num_rows,\n"
                           "       blocks,\n"
                           "       empty_blocks,\n"
                           "       avg_space \"Free space/block\",\n"
                           "       chain_cnt \"Chained rows\",\n"
                           "       avg_row_len \"Average row length\",\n"
                           "       sample_size,\n"
                           "       last_analyzed\n"
                           "  from sys.all_all_tables\n"
                           " where iot_name is null\n"
                           "   and temporary != 'Y' and secondary = 'N'",
                           "",
                           "0800");
static toSQL SQLListTables7("toAnalyze:ListTables",
                            "select 'TABLE' \"Type\",\n"
                            "       owner,\n"
                            "       table_name,\n"
                            "       num_rows,\n"
                            "       blocks,\n"
                            "       empty_blocks,\n"
                            "       avg_space \"Free space/block\",\n"
                            "       chain_cnt \"Chained rows\",\n"
                            "       avg_row_len \"Average row length\",\n"
                            "       sample_size,\n"
                            "       last_analyzed\n"
                            "  from sys.all_tables\n"
                            " where temporary != 'Y' and secondary = 'N'",
                            "",
                            "0703");

static toSQL SQLListIndex("toAnalyze:ListIndex",
                          "SELECT 'INDEX' \"Type\",\n"
                          "       Owner,\n"
                          "       Index_Name,\n"
                          "       Num_rows,\n"
                          "       Distinct_Keys,\n"
                          "       Leaf_Blocks,\n"
                          "       Avg_Leaf_Blocks_Per_Key,\n"
                          "       Avg_Data_Blocks_Per_Key,\n"
                          "       Clustering_Factor,\n"
                          "       Sample_Size,\n"
                          "       Last_Analyzed\n"
                          "  FROM SYS.ALL_INDEXES\n"
                          " WHERE 1 = 1",
                          "List the available indexes, first three column and binds must be same");

static toSQL SQLListPlans("toAnalyze:ListPlans",
                          "SELECT DISTINCT\n"
                          "       statement_id \"Statement\",\n"
                          "       MAX(timestamp) \"Timestamp\",\n"
                          "       MAX(remarks) \"Remarks\" FROM %1\n"
                          " GROUP BY statement_id",
                          "Display available saved statements. Must have same first "
                          "column and %1");

toAnalyze::toAnalyze(QWidget *main, toConnection &connection)
        : toToolWidget(AnalyzeTool, "analyze.html", main, connection)
{
    Tabs = new QTabWidget(this);
    QVBox *box = new QVBox(Tabs);
    Tabs->addTab(box, tr("Analyze"));

    QToolBar *toolbar = toAllocBar(box, tr("Statistics Manager"));

    new QToolButton(QPixmap(const_cast<const char**>(refresh_xpm)),
                    tr("Refresh"),
                    tr("Refresh"),
                    this, SLOT(refresh()),
                    toolbar);

    toolbar->addSeparator();
    if (toIsOracle(connection))
    {
        Analyzed = new QComboBox(toolbar, TO_TOOLBAR_WIDGET_NAME);
        Analyzed->insertItem(tr("All"));
        Analyzed->insertItem(tr("Not analyzed"));
        Analyzed->insertItem(tr("Analyzed"));
    }
    else
        Analyzed = NULL;

    Schema = new toResultCombo(toolbar, TO_TOOLBAR_WIDGET_NAME);
    Schema->setSelected(tr("All"));
    Schema->additionalItem(tr("All"));
    try
    {
        Schema->query(toSQL::sql(toSQL::TOSQL_USERLIST));
    }
    TOCATCH


    if (toIsOracle(connection))
    {
        Type = new QComboBox(toolbar, TO_TOOLBAR_WIDGET_NAME);
        Type->insertItem(tr("Tables"));
        Type->insertItem(tr("Indexes"));

        toolbar->addSeparator();

        Operation = new QComboBox(toolbar, TO_TOOLBAR_WIDGET_NAME);
        Operation->insertItem(tr("Compute statistics"));
        Operation->insertItem(tr("Estimate statistics"));
        Operation->insertItem(tr("Delete statistics"));
        Operation->insertItem(tr("Validate references"));
        connect(Operation, SIGNAL(activated(int)),
                this, SLOT(changeOperation(int)));

        new QLabel(" " + tr("for") + " ", toolbar, TO_TOOLBAR_WIDGET_NAME);
        For = new QComboBox(toolbar, TO_TOOLBAR_WIDGET_NAME);
        For->insertItem(tr("All"));
        For->insertItem(tr("Table"));
        For->insertItem(tr("Indexed columns"));
        For->insertItem(tr("Local indexes"));
        toolbar->addSeparator();
        new QLabel(tr("Sample") + " ", toolbar, TO_TOOLBAR_WIDGET_NAME);
        Sample = new QSpinBox(1, 100, 1, toolbar, TO_TOOLBAR_WIDGET_NAME);
        Sample->setValue(100);
        Sample->setSuffix(" " + tr("%"));
        Sample->setEnabled(false);
    }
    else
    {
        Operation = new QComboBox(toolbar, TO_TOOLBAR_WIDGET_NAME);
        Operation->insertItem(tr("Analyze table"));
        Operation->insertItem(tr("Optimize table"));
        connect(Operation, SIGNAL(activated(int)),
                this, SLOT(changeOperation(int)));

        Type = NULL;
        Sample = NULL;
        For = NULL;
    }

    toolbar->addSeparator();
    new QLabel(tr("Parallel") + " ", toolbar, TO_TOOLBAR_WIDGET_NAME);
    Parallel = new QSpinBox(1, 100, 1, toolbar, TO_TOOLBAR_WIDGET_NAME);
    toolbar->addSeparator();

    new QToolButton(QPixmap(const_cast<const char**>(execute_xpm)),
                    tr("Start analyzing"),
                    tr("Start analyzing"),
                    this, SLOT(execute()),
                    toolbar);
    new QToolButton(QPixmap(const_cast<const char**>(sql_xpm)),
                    tr("Display SQL"),
                    tr("Display SQL"),
                    this, SLOT(displaySQL()),
                    toolbar);

    Current = new QLabel(toolbar, TO_TOOLBAR_WIDGET_NAME);
    Current->setAlignment(AlignRight | AlignVCenter | ExpandTabs);
    toolbar->setStretchableWidget(Current);

    Stop = new QToolButton(QPixmap(const_cast<const char**>(stop_xpm)),
                           tr("Stop current run"),
                           tr("Stop current run"),
                           this, SLOT(stop()),
                           toolbar);
    Stop->setEnabled(false);

    Statistics = new toResultLong(true, false, toQuery::Background, box);
    Statistics->setSelectionMode(QListView::Extended);
    Statistics->setReadAll(true);
    connect(Statistics, SIGNAL(done()), this, SLOT(fillOwner()));
    connect(Statistics, SIGNAL(displayMenu(QPopupMenu *)), this, SLOT(displayMenu(QPopupMenu *)));

    if (Analyzed)
        connect(Analyzed, SIGNAL(activated(int)), this, SLOT(refresh()));
    connect(Schema, SIGNAL(activated(int)), this, SLOT(refresh()));
    if (Type)
        connect(Type, SIGNAL(activated(int)), this, SLOT(refresh()));

    ToolMenu = NULL;
    connect(toMainWidget()->workspace(), SIGNAL(windowActivated(QWidget *)),
            this, SLOT(windowActivated(QWidget *)));

    connect(&Poll, SIGNAL(timeout()), this, SLOT(poll()));

    if (toIsOracle(connection))
    {
        box = new QVBox(Tabs);
        toolbar = toAllocBar(box, tr("Explain plans"));

        Tabs->addTab(box, tr("Explain plans"));
        QSplitter *splitter = new QSplitter(Horizontal, box);
        Plans = new toResultLong(false, false, toQuery::Background, splitter);
        Plans->setSelectionMode(QListView::Single);
        try
        {
            Plans->query(toSQL::string(SQLListPlans,
                                       connection).arg(toConfigurationSingle::Instance().globalConfig(CONF_PLAN_TABLE,
                                                                            DEFAULT_PLAN_TABLE)));
        }
        TOCATCH

        connect(Plans, SIGNAL(selectionChanged()),
                this, SLOT(selectPlan()));
        new QToolButton(QPixmap(const_cast<const char**>(refresh_xpm)),
                        tr("Refresh"),
                        tr("Refresh"),
                        Plans, SLOT(refresh()),
                        toolbar);
        toolbar->setStretchableWidget(new QLabel(toolbar, TO_TOOLBAR_WIDGET_NAME));

        CurrentPlan = new toResultPlan(splitter);

        Worksheet = new toWorksheetStatistic(Tabs);
        Tabs->addTab(Worksheet, tr("Worksheet statistics"));
    }
    else
    {
        Plans = NULL;
        CurrentPlan = NULL;
        Worksheet = NULL;
    }

    refresh();
    setFocusProxy(Tabs);
}

void toAnalyze::fillOwner(void)
{
    for (QListViewItem *item = Statistics->firstChild();item;item = item->nextSibling())
    {
        if (toUnnull(item->text(0)).isNull())
            item->setText(0, Schema->selected());
    }
}

void toAnalyze::selectPlan(void)
{
    QListViewItem *item = Plans->selectedItem();
    if (item)
        CurrentPlan->query("SAVED:" + item->text(0));
}

void toAnalyze::windowActivated(QWidget *widget)
{
    if (widget == this)
    {
        if (!ToolMenu)
        {
            ToolMenu = new QPopupMenu(this);
            ToolMenu->insertItem(QPixmap(const_cast<const char**>(refresh_xpm)), tr("&Refresh"),
                                 this, SLOT(refresh(void)), toKeySequence(tr("F5", "Statistics|Refresh")));

            toMainWidget()->menuBar()->insertItem(tr("&Statistics"), ToolMenu, -1, toToolMenuIndex());
        }
    }
    else
    {
        delete ToolMenu;
        ToolMenu = NULL;
    }
}

toWorksheetStatistic *toAnalyze::worksheet(void)
{
    Tabs->showPage(Worksheet);
    return Worksheet;
}

void toAnalyze::changeOperation(int op)
{
    if (Sample)
        Sample->setEnabled(op == 1);
    if (For)
        For->setEnabled(op == 0 || op == 1);
}

void toAnalyze::refresh(void)
{
    try
    {
        Statistics->setSQL(QString::null);
        toQList par;
        QString sql;
        if (!Type || Type->currentItem() == 0)
            sql = toSQL::string(SQLListTables, connection());
        else
            sql = toSQL::string(SQLListIndex, connection());
        if (Schema->selected() != tr("All"))
        {
            par.insert(par.end(), Schema->selected());
            if (toIsOracle(connection()))
                sql += "\n   AND owner = :own<char[100]>";
            else
                sql += " FROM :f1<noquote>";
        }
        else if (toIsMySQL(connection()))
            sql += " FROM :f1<alldatabases>";
        if (Analyzed)
        {
            switch (Analyzed->currentItem())
            {
            default:
                break;
            case 1:
                sql += QString::fromLatin1("\n  AND Last_Analyzed IS NULL");
                break;
            case 2:
                sql += QString::fromLatin1("\n  AND Last_Analyzed IS NOT NULL");
                break;
            }
        }

        Statistics->query(sql, (const toQList &)par);
    }
    TOCATCH
}

void toAnalyze::poll(void)
{
    try
    {
        int running = 0;
        for (std::list<toNoBlockQuery *>::iterator i = Running.begin();i != Running.end();i++)
        {
            bool eof = false;

            try
            {
                if ((*i)->poll())
                {
                    int cols = (*i)->describe().size();
                    for (int j = 0;j < cols;j++)
                        (*i)->readValueNull();  // Eat the output if any.
                }

                try
                {
                    eof = (*i)->eof();
                }
                catch (const QString &)
                {
                    eof = true;
                }
            }
            catch (const QString &err)
            {
                toStatusMessage(err);
                eof = true;
            }
            if (eof)
            {
                QString sql = toShift(Pending);
                if (!sql.isEmpty())
                {
                    delete (*i);
                    toQList par;
                    (*i) = new toNoBlockQuery(connection(), sql, par);
                    running++;
                }
            }
            else
                running++;
        }
        if (!running)
        {
            Poll.stop();
            refresh();
            stop();
        }
        else
            Current->setText(tr("Running %1 Pending %2").arg(running).arg(Pending.size()));
    }
    TOCATCH
}

std::list<QString> toAnalyze::getSQL(void)
{
    std::list<QString> ret;
    for (QListViewItem *item = Statistics->firstChild();item;item = item->nextSibling())
    {
        if (item->isSelected())
        {
            if (toIsOracle(connection()))
            {
                QString sql = QString::fromLatin1("ANALYZE %3 %1.%2 ");
                QString forc;
                if (item->text(0) == QString::fromLatin1("TABLE"))
                {
                    switch (For->currentItem())
                    {
                    case 0:
                        forc = QString::null;
                        break;
                    case 1:
                        forc = QString::fromLatin1(" FOR TABLE");
                        break;
                    case 2:
                        forc = QString::fromLatin1(" FOR ALL INDEXED COLUMNS");
                        break;
                    case 3:
                        forc = QString::fromLatin1(" FOR ALL LOCAL INDEXES");
                        break;
                    }
                }

                switch (Operation->currentItem())
                {
                case 0:
                    sql += QString::fromLatin1("COMPUTE STATISTICS");
                    sql += forc;
                    break;
                case 1:
                    sql += QString::fromLatin1("ESTIMATE STATISTICS");
                    sql += forc;
                    sql += QString::fromLatin1(" SAMPLE %1 PERCENT").arg(Sample->value());
                    break;
                case 2:
                    sql += QString::fromLatin1("DELETE STATISTICS");
                    break;
                case 3:
                    sql += QString::fromLatin1("VALIDATE REF UPDATE");
                    break;
                }
                toPush(ret, sql.arg(item->text(1)).arg(item->text(2)).arg(item->text(0)));

            }
            else
            {
                QString sql;
                switch (Operation->currentItem())
                {
                case 0:
                    sql = QString::fromLatin1("ANALYZE TABLE %1.%2 ");
                    break;
                case 1:
                    sql = QString::fromLatin1("OPTIMIZE TABLE %1.%2 ");
                    break;
                }
                QString owner = item->text(0);
                if (toUnnull(owner).isNull())
                    owner = Schema->selected();
                toPush(ret, sql.arg(owner).arg(item->text(1)));
            }
        }
    }
    return ret;
}

void toAnalyze::displaySQL(void)
{
    QString txt;
    std::list<QString> sql = getSQL();
    for (std::list<QString>::iterator i = sql.begin();i != sql.end();i++)
        txt += (*i) + ";\n";
    new toMemoEditor(this, txt, -1, -1, true);
}

void toAnalyze::execute(void)
{
    stop();

    std::list<QString> sql = getSQL();
    for (std::list<QString>::iterator i = sql.begin();i != sql.end();i++)
        toPush(Pending, *i);

    try
    {
        toQList par;
        for (int i = 0;i < Parallel->value();i++)
        {
            QString sql = toShift(Pending);
            if (!sql.isEmpty())
                toPush(Running, new toNoBlockQuery(connection(), sql, par));
        }
        Poll.start(100);
        Stop->setEnabled(true);
        poll();
    }
    TOCATCH
}

void toAnalyze::stop(void)
{
    try
    {
        for_each(Running.begin(), Running.end(), DeleteObject());
        Running.clear();
        Pending.clear();
        Stop->setEnabled(false);
        Current->setText(QString::null);
        if (!connection().needCommit())
        {
            try
            {
                connection().rollback();
            }
            catch (...)
            { }
        }
    }
    TOCATCH
}

void toAnalyze::createTool(void)
{
    AnalyzeTool.createWindow();
}

void toAnalyze::displayMenu(QPopupMenu *menu)
{
    menu->insertSeparator(0);
    menu->insertItem(QPixmap(const_cast<const char**>(sql_xpm)), tr("Display SQL"), this, SLOT(displaySQL()), 0, 0, 0);
    menu->insertItem(QPixmap(const_cast<const char**>(execute_xpm)), tr("Execute"), this, SLOT(execute()), 0, 0, 0);
    menu->insertSeparator(0);
    menu->insertItem(QPixmap(const_cast<const char**>(refresh_xpm)), tr("Refresh"), this, SLOT(refresh()), 0, 0, 0);
}