Changeset 19090 for trunk/FACT++/src


Ignore:
Timestamp:
07/26/18 20:06:53 (6 years ago)
Author:
tbretz
Message:
A very simple implementation which will EXPLAIN the query with what MySQL offers -- some formatting might help to read the output, but I found no easy way to do that.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/rootifysql.cc

    r19079 r19090  
    3636        ("no-fill",       po_switch(),                 "Do not fill events into the root file (mainly for debugging purposes, e.g. performance studies)")
    3737        ("delimiter",     var<string>(""),             "The delimiter used if contents are displayed with --display (default=\\t)")
     38        ("explain",       po_switch(),                 "Requests an EXPLAIN from the server (shows the serveroptimized query)")
    3839        ("var.*",         var<string>(),               "Predefined SQL user variables (@VAR)")
    3940        ("env.*",         vars<string>(),              "Predefined environment for substitutions in the query ($ENV)")
     
    123124    const bool     noout       = conf.Get<bool>("null");
    124125    const bool     nofill      = conf.Get<bool>("no-fill");
     126    const bool     explain     = conf.Get<bool>("explain");
    125127    const uint16_t verbose     = conf.Get<uint16_t>("verbose");
    126128    const uint16_t compression = conf.Get<uint16_t>("compression");
     
    244246    }
    245247
     248    // ------------------------- Explain query if requested --------------------
     249
     250    if (explain)
     251    {
     252        try
     253        {
     254            const auto res0 =
     255                connection.query("EXPLAIN FORMAT=JSON "+query).store();
     256
     257            cout << res0[0][0] << endl;
     258            cout << endl;
     259
     260            const mysqlpp::StoreQueryResult res1 =
     261                connection.query("EXPLAIN "+query).store();
     262
     263            for (size_t i=0; i<res1.num_rows(); i++)
     264            {
     265                const mysqlpp::Row &row = res1[i];
     266
     267                cout << "\nid           : " << row["id"];
     268                cout << "\nselect type  : " << row["select_type"];
     269
     270                if (!row["table"].is_null())
     271                    cout << "\ntable        : " << row["table"];
     272
     273                if (!row["partitions"].is_null())
     274                    cout << "\npartitions   : " << row["partitions"];
     275
     276                if (!row["key"].is_null())
     277                    cout << "\nselected key : " << row["key"] << " [len=" << row["key_len"] << "] out of (" << row["possible_keys"] << ")";
     278
     279                if (!row["type"].is_null())
     280                    cout << "\njoin type    : " << row["type"];
     281
     282                //if (!row["possible_keys"].is_null())
     283                //    cout << "\npossible_keys: " << row["possible_keys"];
     284
     285                //if (!row["key_len"].is_null())
     286                //    cout << "\nkey_len      : " << row["key_len"];
     287
     288                if (!row["ref"].is_null())
     289                    cout << "\nref          : (" << row["ref"] << ") compared to the index";
     290
     291                if (!row["rows"].is_null())
     292                    cout << "\nrows         : " << row["rows"];
     293
     294                if (!row["filtered"].is_null())
     295                    cout << "\nfiltered     : " << row["filtered"];
     296
     297                if (!row["extra"].is_null())
     298                    cout << "\nExtra        : " << row["extra"];
     299
     300                cout << endl;
     301            }
     302
     303            cout << endl;
     304
     305            return 0;
     306
     307            const mysqlpp::StoreQueryResult res2 =
     308                connection.query("SHOW WARNINGS").store();
     309
     310            for (size_t i=0; i<res2.num_rows(); i++)
     311            {
     312                const mysqlpp::Row &row = res2[i];
     313
     314                // 1003 //
     315                cout << row["Level"] << '[' << row["Code"] << "]:\n";
     316                cout << row["Message"] << '\n' << endl;
     317
     318            }
     319
     320        }
     321        catch (const exception &e)
     322        {
     323            cerr << query << "\n\n";
     324            cerr << "SQL query failed:\n" << e.what() << endl;
     325            return 6;
     326        }
     327
     328        return 0;
     329    }
     330
    246331    // -------------------------- Request data from database -------------------
    247332    if (verbose>0)
     
    262347    TFile tfile(path, update?"UPDATE":(force?"RECREATE":"CREATE"), "Rootify SQL", compression);
    263348    if (tfile.IsZombie())
    264         return 6;
     349        return 7;
    265350
    266351    // -------------------------------------------------------------------------
     
    271356    {
    272357        cerr << "Empty set returned... nothing to write." << endl;
    273         return 7;
     358        return 8;
    274359    }
    275360
Note: See TracChangeset for help on using the changeset viewer.