How to insert 100000 records in a schema - php

I'm using Php 5.4, mySql 5.6
I have a schema that I need to fill with 100000s entries in a single query (for testing purpose). I have some problems because the server takes too long to elaborate, so it reaches the time limit for the elaboration. Or it directly fails the call to the database, sending a message of general fatal error without any further explanation.
My question is simple: why? Are there some php parameters that have to be changed?
Moreover, is better to run a single insert more that 1000000 times or a single multi-insert query with 1000000 records?
P.S.: if I cycle 13 times a single instance which inserts 8000 records everything it's ok and it doesn't take so long...

You could do this, for example:
<?php
$x=1;
$col1 = 1;
$col2 = 1;
$col3 = 1;
$quantity = 1000000-1;
$query = "INSERT INTO `table` (`col1`, `col2`, `col3`) VALUES ('$col1', '$col2', '$col3') ";
while ($x <= $quantity)
{
$col1++;
$col2++;
$col3++;
$x++;
$query .= ", ('$col1', '$col2', '$col3') ";
}
echo $query;
?>
Changing quantity to 100-1 generates this:
INSERT INTO `table` (`col1`, `col2`, `col3`) VALUES ('1', '1', '1') ,
('2', '2', '2') , ('3', '3', '3') , ('4', '4', '4') , ('5', '5', '5')
, ('6', '6', '6') , ('7', '7', '7') , ('8', '8', '8') , ('9', '9',
'9') , ('10', '10', '10') , ('11', '11', '11') , ('12', '12', '12') ,
('13', '13', '13') , ('14', '14', '14') , ('15', '15', '15') , ('16',
'16', '16') , ('17', '17', '17') , ('18', '18', '18') , ('19', '19',
'19') , ('20', '20', '20') , ('21', '21', '21') , ('22', '22', '22') ,
('23', '23', '23') , ('24', '24', '24') , ('25', '25', '25') , ('26',
'26', '26') , ('27', '27', '27') , ('28', '28', '28') , ('29', '29',
'29') , ('30', '30', '30') , ('31', '31', '31') , ('32', '32', '32') ,
('33', '33', '33') , ('34', '34', '34') , ('35', '35', '35') , ('36',
'36', '36') , ('37', '37', '37') , ('38', '38', '38') , ('39', '39',
'39') , ('40', '40', '40') , ('41', '41', '41') , ('42', '42', '42') ,
('43', '43', '43') , ('44', '44', '44') , ('45', '45', '45') , ('46',
'46', '46') , ('47', '47', '47') , ('48', '48', '48') , ('49', '49',
'49') , ('50', '50', '50') , ('51', '51', '51') , ('52', '52', '52') ,
('53', '53', '53') , ('54', '54', '54') , ('55', '55', '55') , ('56',
'56', '56') , ('57', '57', '57') , ('58', '58', '58') , ('59', '59',
'59') , ('60', '60', '60') , ('61', '61', '61') , ('62', '62', '62') ,
('63', '63', '63') , ('64', '64', '64') , ('65', '65', '65') , ('66',
'66', '66') , ('67', '67', '67') , ('68', '68', '68') , ('69', '69',
'69') , ('70', '70', '70') , ('71', '71', '71') , ('72', '72', '72') ,
('73', '73', '73') , ('74', '74', '74') , ('75', '75', '75') , ('76',
'76', '76') , ('77', '77', '77') , ('78', '78', '78') , ('79', '79',
'79') , ('80', '80', '80') , ('81', '81', '81') , ('82', '82', '82') ,
('83', '83', '83') , ('84', '84', '84') , ('85', '85', '85') , ('86',
'86', '86') , ('87', '87', '87') , ('88', '88', '88') , ('89', '89',
'89') , ('90', '90', '90') , ('91', '91', '91') , ('92', '92', '92') ,
('93', '93', '93') , ('94', '94', '94') , ('95', '95', '95') , ('96',
'96', '96') , ('97', '97', '97') , ('98', '98', '98') , ('99', '99',
'99') , ('100', '100', '100')
It's not adviced to do so. It's going to take a while to run and I'm not sure if it will work, you can increase the timeout for the page to make sure it runs.
But since it's for testing purposes..
You can change the values of the col variables if you want, but you can't use $var++ on strings.

Is the problem that the test takes longer than the 30 seconds or so that PHP allows before killing the process?
If so, you can set max_execution_time in php.ini.
See
http://php.net/manual/en/info.configuration.php#ini.max-execution-time
http://php.net/manual/en/function.set-time-limit.php

You could try to minimise the traffic between the application server and the SQL engine:
Start by inserting just one record, and then in a PHP loop, double the number of records in each iteration:
First record:
INSERT INTO TABLE mytable (txt) VALUES ('Test');
In a loop:
INSERT INTO TABLE mytable (txt)
SELECT txt FROM mytable;
If you execute this loop 20 times, you'll end up with 220 records, i.e. ~ 1000000
If you have an AUTO_INCREMENT id field, it will of course be filled with unique values. You can also increment another numerical field explicitly, like this:
INSERT INTO TABLE mytable (num) VALUES (1);
In a loop (PHP):
$count = 1;
foreach (range(1, 20) as $i) {
$sql = "INSERT INTO TABLE mytable (num)
SELECT num + $count FROM mytable";
$mysqli->query($sql);
$count += $count;
}

You should insert this one by one for best perfomance and for more clear statement. You should also remove any index or constrains as well as use MyISAM instead of innodb for fastest sql insert. Best is to use .sql file and console input.

Related

Column count doesn't match value count at row 1 error message, php / sql [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have to make a webpage using PHP and MySQL workbench to collect form data and I keep getting this error:
INSERT INTO tools ( id, tool1, tool2, tool3, tool4, tool5, tool6, tool7, tool8, tool9, tool10) VALUES ( '0','1', '2', '3', '4,' '5', '6', '7', '8', '9', '10')
INSERT failed: INSERT INTO tools ( id, tool1, tool2, tool3, tool4, tool5, tool6, tool7, tool8, tool9, tool10) VALUES ( '0','1', '2', '3', '4,' '5', '6', '7', '8', '9', '10')
Column count doesn't match value count at row 1
This is what my code looks like:
$query = "INSERT INTO tools ( id, tool1, tool2, tool3, tool4, tool5, tool6, tool7, tool8, tool9, tool10) VALUES ( '0','1', '2', '3', '4,' '5', '6', '7', '8', '9', '10')";
and this is what it looks like in workbench:
INSERT INTO tools (id, tool1, tool2, tool3, tool4, tool5, tool6, tool7, tool8, tool9, tool10) VALUES ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
I've tried it with an without the id and the 0 value, and neither work. Does anyone know how to fix this?
Your code
INSERT INTO tools ( id, tool1, tool2, tool3, tool4, tool5, tool6, tool7, tool8, tool9, tool10)
VALUES ( '0','1', '2', '3', '4,' '5', '6', '7', '8', '9', '10')
Has an error exactly between 4 and 5 the apostrophes are misspaced
It must look like this
INSERT INTO tools ( id, tool1, tool2, tool3, tool4, tool5, tool6, tool7, tool8, tool9, tool10)
VALUES ( '0','1', '2', '3', '4', '5', '6', '7', '8', '9', '10')

CI Is there a way to query custom order_by

Is it possible to customize the order_by query in MySQL or in CI ? Such as I want my column to be ordered by ('1', '11', '4', '2', '21', '3', '5', '7') So if I query it as ASC the result will show in the order of my customized order.
If it is not possible, what is the best workaround to get these order ? Hoping for a simple solution just using the MySQL query.
All answers and suggestions are greatly welcomed. Thanks.
Try this one.
$this -> db -> order_by('FIELD ( table.id, 1, 11, 4,2,21,3,5,7 )');
link
Pure Mysql answer is yes you can order a field by a set list with the MYSQL FIELD() function
SELECT *
FROM mytable
WHERE id IN ('1', '11', '4', '2', '21', '3', '5', '7')
ORDER BY FIELD(id, '1', '11', '4', '2', '21', '3', '5', '7')

how to display attendance report based on roll number and i want everything to be dynamic [duplicate]

This question already has an answer here:
MySQL pivot row into dynamic number of columns
(1 answer)
Closed 5 years ago.
I am working on Classes management project. Everything is done successfully but only stuck with attendance report work.
i want result like this
My table structure:
CREATE TABLE mp_attendence (atid int, atdisplayname varchar(9), atroll varchar(9), atstatus varchar(9), atdate date, atbatch varchar(9)) ;
INSERT INTO mp_attendence (atid, atdisplayname, atroll, atstatus, atdate, atbatch) VALUES (1, 'lavkush', '1', 'absent', '2017-03-01', 'p1'), (2, 'dhanji', '2', 'present', '2017-03-01', 'p1'), (3, 'lavkush', '1', 'present', '2017-03-02', 'p1'), (4, 'dhanji', '2', 'present', '2017-03-02', 'p1'), (5, 'lavkush', '1', 'present', '2017-03-04', 'p1'), (6, 'dhanji', '2', 'absent', '2017-03-04', 'p1'), (7, 'lavkush', '1', 'present', '2017-03-05', 'p1'), (8, 'dhanji', '2', 'absent', '2017-03-05', 'p1') ;
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'(case when atstatus = ''',
atstatus,
''' then (atstatus) end) AS ',
replace (DATE_FORMAT(atdate, '%d %M %Y'), ' ', '')
)
) INTO #sql
from mp_attendence;
SET #sql = CONCAT('SELECT atdisplayname, atroll, ', #sql, ' from mp_attendence
group by atroll');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
From above query i achieve this result but i want to combine same dates like 1st march result should be in same row.
result achieved

The insert query working in mysql phpmyadmin isn't working when I try it using php mysqli or wpdb

I am trying to insert a row into a table using $wpdb. When i run the code
$wpdb->insert( 'sync_orders', $data);
$last_query = $wpdb->last_query;
It shows error 'you have an error in mysql syntax'. I am fetching the last query and when I run that query into phpmyadmin, the query is working fine.
I thought that $wpdb is having some problem, so I tried that same query using mysqli_query(). But here again I got same problem.
Any help will be appreciated.
P.S. : not using the third param $format in $wpdb->insert() as I am inserting all the values as string.
This is the query I am getting from $wpdb
INSERT INTO `sync_orders` (`post_author`, `post_title`, `post_date`, `post_status`, `order_key`, `order_currency`, `prices_include_tax`, `customer_ip_address`, `customer_user_agent`, `customer_user`, `created_via`, `cart_hash`, `order_version`, `billing_first_name`, `billing_last_name`, `billing_company`, `billing_email`, `billing_phone`, `billing_country`, `billing_address_1`, `billing_address_2`, `billing_city`, `billing_state`, `billing_postcode`, `shipping_first_name`, `shipping_last_name`, `shipping_company`, `shipping_country`, `shipping_address_1`, `shipping_address_2`, `shipping_city`, `shipping_state`, `shipping_postcode`, `payment_method`, `payment_method_title`, `order_shipping`, `cart_discount`, `cart_discount_tax`, `order_tax`, `order_shipping_tax`, `order_total`, `download_permissions_granted`, `recorded_sales`, `order_stock_reduced`, `edit_lock`, `order_item_name`, `order_item_type`, `qty`, `tax_class`, `product_id`, `variation_id`, `line_subtotal`, `line_total`, `line_subtotal_tax`, `line_tax`, `line_tax_data`, `pa_color`, `pa_size`) VALUES (1, 'Order – February 3, 2017 # 01:08 PM', '2017-02-03 13:08:50', 'wc-processing', 'wc_order_58948091938b0', 'INR', 'no', '127.0.0.1', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0', '1', 'checkout', '3229e8ee0da4d8ed89d879f4a1ba9dea', '2.6.13', 'sasdf', 'asdf', 'asdfasdf asdf', 'sharad.kalya#techinfini.in', '7565465455', 'IN', 'asdff adf ffasdf', 'asdfsadf sadf', 'goa', 'GA', '454545', 'sasdf', 'asdf', 'asdfasdf asdf', 'IN', 'asdff adf ffasdf', 'asdfsadf sadf', 'goa', 'GA', '454545', 'cod', 'Cash on Delivery', '', '0', '0', '0', '0', '450.00', '1', 'yes', '1', '1487087216:1', 'Product - 1', 'line_item', '1', '', '9', '20', '450', '450', '0', '0', 'a:2:{s:5:\\\"total\\\";a:0:{}s:8:\\\"subtotal\\\";a:0:{}}', 'brown', 'medium')

I need to change a complex MYSQL query in to a view or views

I have a query that creates a rolling sum and subtracts that from a sum total. I have the query working in the SQL window, but I need some help changing it in to a view or table that I can call from a PHP so I can use the results in building a graph.
This query builds the basis for a burndown report used in project management so it is very handy.
When I try to create a view out it I get multiple errors with joins and variables not being allowed in a view. I am kind of a mysql newbie and I am beating my head on this one.
Here is the SQL query:
SELECT
taskid,
projectid,
esthours,
actualhours,
eview.SE As TotalHours,
(#EST:=#EST - esthours) as ESTI,
(eview.SE + #EST) as Estimated,
(#EST2:=#EST2 - actualhours) as AC,
(eview.SE + #EST2) as Actual
from
tbltasks,
eview
JOIN
(SELECT #EST:=0) EE
JOIN
(SELECT #EST2:=0) E2;
Here is a sample of the results.
taskid, projectid, esthours, actualhours, TotalHours, ESTI,Estimated, AC, Actual
'1021', '2', '4', '3', '20', '-4', '16', '-3', '17'
'1022', '2', '3', '3', '20', '-7', '13', '-6', '14'
'1023', '2', '2', '4', '20', '-9', '11', '-10', '10'

Categories