并行运行多个数据库查询

时间:2014-04-16 11:22:32

标签: mysql node.js

下面的脚本用于测试以更快的速度克隆数据库的选项。

它使用节点js并行运行多个数据库查询。到目前为止,结果是一样的。似乎一次打开了与服务器的多个连接,但查询仍按顺序执行。

var mysql = require('mysql')
async = require('async');

// get list of tables
//
var uuid = 'def';
var default_db = 'abc';

var makeConnection = function () {
    return mysql.createConnection({
                host: 'localhost',
                user: 'root',
                password: 'password',
                multipleStatements: true
            });
}   

makeConnection().query('SHOW TABLES from ' + uuid, function(err, rows, fields) {
    if (err) throw err;

    var parallel = 5;
    var parallels = [], tables = [];
    var tables_count_from_zero = rows.length - 1;
    var tables_per_parallel = Math.ceil(rows.length / parallel);

    var makeQuery = function makeQuery (tables) { // factory function to create the queries
        return function doQuery (callback) {

            console.log('start');
            makeConnection().query('SET foreign_key_checks = 0;' + tables.join(''), function(err, rows, fields) {
                    if (err) throw err;
                    console.log('done');
                    callback(err, null);
                });
        };
    };

    var counter = 0;    
    for (row in rows) {
        counter++;

        var table_name = rows[row]['Tables_in_nilead'];

        tables.push('CREATE TABLE IF NOT EXISTS ' + uuid + '.' + table_name + ' LIKE ' + default_db + '.' + table_name + ';');
        tables.push('INSERT ' + uuid + '.' + table_name + ' SELECT * FROM ' + default_db + '.' + table_name + ';');


        if (counter >= tables_per_parallel || row >= tables_count_from_zero) {
            counter = 0;            

            parallels.push(makeQuery(tables));

            tables = [];

            console.log('pushed ok');
        }
    }

    var initTimer = Date.now();

    async.parallel(parallels,

        // optional callback

        function(err, results) {
            console.log('Done ' + results + ' at: ' + (Date.now() - initTimer));
            // the results array will equal ['one','two'] even though
            // the second function had a shorter timeout.
        }
    );
});

//编辑:更多测试: 转--innodb-flush-log-at-trx-commit为0或2加速至少3次

//编辑以按要求添加控制台日志结果

  

CREATE TABLE IF NOT NOT EXISTS def.acl_classes LIKE   mydb.acl_classes; INSERT def.acl_classes SELECT * FROM   mydb.acl_classes; CREATE TABLE IF NOT NOT EXISTS def.acl_entries LIKE   mydb.acl_entries; INSERT def.acl_entries SELECT * FROM   mydb.acl_entries; CREATE TABLE IF NOT NOT EXISTS def.acl_object_identities   像mydb.acl_object_identities; INSERT def.acl_object_identities   SELECT * FROM mydb.acl_object_identities; CREATE TABLE IF NOT NOT EXISTS   def.acl_object_identity_ancestors LIKE   mydb.acl_object_identity_ancestors; INSERT   def.acl_object_identity_ancestors SELECT * FROM   mydb.acl_object_identity_ancestors; CREATE TABLE IF NOT NOT EXISTS   def.acl_security_identities LIKE mydb.acl_security_identities; INSERT   def.acl_security_identities SELECT * FROM   mydb.acl_security_identities;如果不是EXISTS则创建表   def.association LIKE mydb.association; INSERT def.association SELECT *   FROM mydb.association; CREATE TABLE IF NOT NOT EXISTS def.association_type   LIKE mydb.association_type; INSERT def.association_type SELECT * FROM   mydb.association_type; CREATE TABLE IF NOT NOT EXISTS def.cart LIKE   mydb.cart; INSERT def.cart SELECT * FROM mydb.cart; CREATE TABLE IF NOT   EXISTS def.cart_item LIKE mydb.cart_item; INSERT def.cart_item SELECT *   FROM mydb.cart_item; CREATE TABLE IF NOT NOT EXISTS def.configuration LIKE   mydb.configuration; INSERT def.configuration SELECT * FROM   mydb.configuration; CREATE TABLE IF NOT NOT EXISTS def.contact_mechanism   像mydb.contact_mechanism; INSERT def.contact_mechanism SELECT * FROM   mydb.contact_mechanism; CREATE TABLE IF NOT NOT EXISTS   def.contact_mechanism_link LIKE mydb.contact_mechanism_link; INSERT   def.contact_mechanism_link SELECT * FROM   mydb.contact_mechanism_link; CREATE TABLE IF NOT NOT EXISTS   def.contact_mechanism_purpose_type LIKE   mydb.contact_mechanism_purpose_type; INSERT   def.contact_mechanism_purpose_type SELECT * FROM   mydb.contact_mechanism_purpose_type; CREATE TABLE IF NOT NOT EXISTS   def.contact_mechanism_type LIKE mydb.contact_mechanism_type; INSERT   def.contact_mechanism_type SELECT * FROM   mydb.contact_mechanism_type; CREATE TABLE IF NOT NOT EXISTS def.container   像mydb.container; INSERT def.container SELECT * FROM   mydb.container; CREATE TABLE IF NOT NOT EXISTS def.container_type LIKE   mydb.container_type; INSERT def.container_type SELECT * FROM   mydb.container_type; CREATE TABLE IF NOT NOT EXISTS def.credit_card LIKE   mydb.credit_card; INSERT def.credit_card SELECT * FROM   mydb.credit_card; CREATE TABLE IF NOT NOT EXISTS def.currency LIKE   mydb.currency; INSERT def.currency SELECT * FROM mydb.currency;

     

启动

     

CREATE TABLE IF NOT NOT EXISTS def.discount_price_component LIKE   mydb.discount_price_component; INSERT def.discount_price_component   SELECT * FROM mydb.discount_price_component; CREATE TABLE IF NOT NOT EXISTS   def.domain LIKE mydb.domain; INSERT def.domain SELECT * FROM   mydb.domain; CREATE TABLE IF NOT NOT EXISTS def.facility LIKE   mydb.facility; INSERT def.facility SELECT * FROM mydb.facility; CREATE   表IF NOT NOT EXISTS def.geo_boundary LIKE mydb.geo_boundary; INSERT   def.geo_boundary SELECT * FROM mydb.geo_boundary; CREATE TABLE IF NOT   EXISTS def.geo_boundary_association LIKE   mydb.geo_boundary_association; INSERT def.geo_boundary_association   SELECT * FROM mydb.geo_boundary_association; CREATE TABLE IF NOT NOT EXISTS   def.group LIKE mydb.group; INSERT def.group SELECT * FROM   mydb.group; CREATE TABLE IF NOT NOT EXISTS def.guide LIKE mydb.guide; INSERT   def.guide SELECT * FROM mydb.guide; CREATE TABLE IF NOT NOT EXISTS   def.guide_item LIKE mydb.guide_item; INSERT def.guide_item SELECT *   来自mydb.guide_item;创建表,如果不是EXISTS def.image LIKE   mydb.image; INSERT def.image SELECT * FROM mydb.image; CREATE TABLE IF   NOT EXISTS def.inventory_item LIKE mydb.inventory_item; INSERT   def.inventory_item SELECT * FROM mydb.inventory_item; CREATE TABLE IF   NOT EXISTS def.inventory_item_adjustment LIKE   mydb.inventory_item_adjustment; INSERT def.inventory_item_adjustment   SELECT * FROM mydb.inventory_item_adjustment;如果不是,则创建表   EXISTS def.inventory_item_variance LIKE   mydb.inventory_item_variance; INSERT def.inventory_item_variance SELECT   * FROM mydb.inventory_item_variance; CREATE TABLE IF NOT NOT EXISTS def.inventory_item_variance_reason LIKE   mydb.inventory_item_variance_reason; INSERT   def.inventory_item_variance_reason SELECT * FROM   mydb.inventory_item_variance_reason; CREATE TABLE IF NOT NOT EXISTS   def.invoice LIKE mydb.invoice; INSERT def.invoice SELECT * FROM   mydb.invoice; CREATE TABLE IF NOT NOT EXISTS def.invoice_address LIKE   mydb.invoice_address; INSERT def.invoice_address SELECT * FROM   mydb.invoice_address; CREATE TABLE IF NOT NOT EXISTS def.invoice_item LIKE   mydb.invoice_item; INSERT def.invoice_item SELECT * FROM   mydb.invoice_item; CREATE TABLE IF NOT NOT EXISTS def.language LIKE   mydb.language; INSERT def.language SELECT * FROM mydb.language; CREATE   表IF NOT NOT EXISTS def.message LIKE mydb.message; INSERT def.message   SELECT * FROM mydb.message;

     

启动

     

创建表,如果不是EXISTS   def.message_template LIKE mydb.message_template; INSERT   def.message_template SELECT * FROM mydb.message_template; CREATE TABLE   如果不是EXISTS def.navigation LIKE mydb.navigation; INSERT   def.navigation SELECT * FROM mydb.navigation; CREATE TABLE IF NOT   EXISTS def.option LIKE mydb.option; INSERT def.option SELECT * FROM   mydb.option; CREATE TABLE IF NOT NOT EXISTS def.option_value LIKE   mydb.option_value; INSERT def.option_value SELECT * FROM   mydb.option_value; CREATE TABLE IF NOT NOT EXISTS def.order LIKE   mydb.order; INSERT def.order SELECT * FROM mydb.order; CREATE TABLE IF   NOT EXISTS def.order_address LIKE mydb.order_address; INSERT   def.order_address SELECT * FROM mydb.order_address; CREATE TABLE IF NOT NOT   EXISTS def.order_adjustment LIKE mydb.order_adjustment; INSERT   def.order_adjustment SELECT * FROM mydb.order_adjustment; CREATE TABLE   如果不是EXISTS def.order_item LIKE mydb.order_item; INSERT   def.order_item SELECT * FROM mydb.order_item;如果不是,则创建表   EXISTS def.order_item_address LIKE mydb.order_item_address; INSERT   def.order_item_address SELECT * FROM mydb.order_item_address; CREATE   表IF NOT NOT EXISTS def.order_item_role LIKE   mydb.order_item_role; INSERT def.order_item_role SELECT * FROM   mydb.order_item_role; CREATE TABLE IF NOT NOT EXISTS def.order_role LIKE   mydb.order_role; INSERT def.order_role SELECT * FROM   mydb.order_role; CREATE TABLE IF NOT NOT EXISTS def.page LIKE   mydb.page; INSERT def.page SELECT * FROM mydb.page; CREATE TABLE IF NOT   EXISTS def.party LIKE mydb.party; INSERT def.party SELECT * FROM   mydb.party; CREATE TABLE IF NOT NOT EXISTS def.party_contact_mechanism LIKE   mydb.party_contact_mechanism; INSERT def.party_contact_mechanism SELECT   * FROM mydb.party_contact_mechanism; CREATE TABLE IF NOT NOT EXISTS def.party_contact_mechanism_purpose LIKE   mydb.party_contact_mechanism_purpose; INSERT   def.party_contact_mechanism_purpose SELECT * FROM   mydb.party_contact_mechanism_purpose;创建表,如果不是EXISTS   def.party_relationship LIKE mydb.party_relationship; INSERT   def.party_relationship SELECT * FROM mydb.party_relationship; CREATE   表IF NOT NOT EXISTS def.party_role LIKE mydb.party_role; INSERT   def.party_role SELECT * FROM mydb.party_role; CREATE TABLE IF NOT   EXISTS def.party_role_type LIKE mydb.party_role_type; INSERT   def.party_role_type SELECT * FROM mydb.party_role_type;

     

启动

     

CREATE TABLE IF NOT NOT EXISTS def.payment LIKE mydb.payment; INSERT def.payment   SELECT * FROM mydb.payment; CREATE TABLE IF NOT NOT EXISTS def.payment_log   像mydb.payment_log; INSERT def.payment_log SELECT * FROM   mydb.payment_log; CREATE TABLE IF NOT NOT EXISTS def.payment_method LIKE   mydb.payment_method; INSERT def.payment_method SELECT * FROM   mydb.payment_method; CREATE TABLE IF NOT NOT EXISTS def.post LIKE   mydb.post; INSERT def.post SELECT * FROM mydb.post; CREATE TABLE IF NOT   EXISTS def.product LIKE mydb.product; INSERT def.product SELECT * FROM   mydb.product; CREATE TABLE IF NOT NOT EXISTS def.product_image LIKE   mydb.product_image; INSERT def.product_image SELECT * FROM   mydb.product_image; CREATE TABLE IF NOT NOT EXISTS def.product_option LIKE   mydb.product_option; INSERT def.product_option SELECT * FROM   mydb.product_option; CREATE TABLE IF NOT NOT EXISTS def.product_property   像mydb.product_property; INSERT def.product_property SELECT * FROM   mydb.product_property; CREATE TABLE IF NOT NOT EXISTS def.product_taxon   像mydb.product_taxon; INSERT def.product_taxon SELECT * FROM   mydb.product_taxon; CREATE TABLE IF NOT NOT EXISTS def.property LIKE   mydb.property; INSERT def.property SELECT * FROM mydb.property; CREATE   表IF NOT NOT EXISTS def.seo LIKE mydb.seo; INSERT def.seo SELECT * FROM   mydb.seo;创建表,如果不是EXISTS def.shipment LIKE   mydb.shipment; INSERT def.shipment SELECT * FROM mydb.shipment; CREATE   表IF NOT NOT EXISTS def.shipment_address LIKE   mydb.shipment_address; INSERT def.shipment_address SELECT * FROM   mydb.shipment_address; CREATE TABLE IF NOT NOT EXISTS def.shipment_item   像mydb.shipment_item; INSERT def.shipment_item SELECT * FROM   mydb.shipment_item; CREATE TABLE IF NOT NOT EXISTS   def.shipment_item_inventory_item LIKE   mydb.shipment_item_inventory_item; INSERT   def.shipment_item_inventory_item SELECT * FROM   mydb.shipment_item_inventory_item;如果不是EXISTS则创建表   def.shipping_category LIKE mydb.shipping_category; INSERT   def.shipping_category SELECT * FROM mydb.shipping_category; CREATE   表IF NOT NOT EXISTS def.shipping_method LIKE   mydb.shipping_method; INSERT def.shipping_method SELECT * FROM   mydb.shipping_method; CREATE TABLE IF NOT NOT EXISTS   def.shipping_method_rule LIKE mydb.shipping_method_rule; INSERT   def.shipping_method_rule SELECT * FROM mydb.shipping_method_rule;

     

启动

     

CREATE TABLE IF NOT NOT EXISTS def.shipping_methods_categories LIKE   mydb.shipping_methods_categories; INSERT   def.shipping_methods_categories SELECT * FROM   mydb.shipping_methods_categories;如果不是EXISTS则创建表   def.shipping_methods_zones LIKE mydb.shipping_methods_zones; INSERT   def.shipping_methods_zones SELECT * FROM   mydb.shipping_methods_zones; CREATE TABLE IF NOT NOT EXISTS   def.store_setting LIKE mydb.store_setting; INSERT def.store_setting   SELECT * FROM mydb.store_setting; CREATE TABLE IF NOT NOT EXISTS   def.tax_category LIKE mydb.tax_category; INSERT def.tax_category SELECT   * FROM mydb.tax_category; CREATE TABLE IF NOT NOT EXISTS def.tax_rate LIKE mydb.tax_rate; INSERT def.tax_rate SELECT * FROM mydb.tax_rate; CREATE   表IF NOT NOT EXISTS def.taxon LIKE mydb.taxon; INSERT def.taxon SELECT   * FROM mydb.taxon; CREATE TABLE IF NOT NOT EXISTS def.theme LIKE mydb.theme; INSERT def.theme SELECT * FROM mydb.theme; CREATE TABLE IF   NOT EXISTS def.unit_of_measurement LIKE   mydb.unit_of_measurement; INSERT def.unit_of_measurement SELECT * FROM   mydb.unit_of_measurement; CREATE TABLE IF NOT NOT EXISTS   def.unit_of_measurement_conversion LIKE   mydb.unit_of_measurement_conversion; INSERT   def.unit_of_measurement_conversion SELECT * FROM   mydb.unit_of_measurement_conversion; CREATE TABLE IF NOT NOT EXISTS   def.user LIKE mydb.user; INSERT def.user SELECT * FROM mydb.user; CREATE   表IF NOT NOT EXISTS def.user_group LIKE mydb.user_group; INSERT   def.user_group SELECT * FROM mydb.user_group;如果不是,则创建表   EXISTS def.variant LIKE mydb.variant; INSERT def.variant SELECT * FROM   mydb.variant; CREATE TABLE IF NOT NOT EXISTS def.variant_image LIKE   mydb.variant_image; INSERT def.variant_image SELECT * FROM   mydb.variant_image; CREATE TABLE IF NOT NOT EXISTS def.variant_option_value   LIKE mydb.variant_option_value; INSERT def.variant_option_value SELECT   * FROM mydb.variant_option_value; CREATE TABLE IF NOT NOT EXISTS def.variant_price_component LIKE mydb.variant_price_component; INSERT   def.variant_price_component SELECT * FROM   mydb.variant_price_component;

0 个答案:

没有答案