Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7925485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:18:40+00:00 2026-06-03T18:18:40+00:00

I need to populate a MySQL table with random SHA-1 hash values, generated by

  • 0

I need to populate a MySQL table with random SHA-1 hash values, generated by PHP function.
I`m trying to optimize the insert by splitting it in chunks of 10000.
My question is:
Is the following approach efficient? Here is the code.

//MySQL server connection routines are above this point
if ($select_db) {
$time_start = microtime(true);
//query
$query = 'INSERT INTO sha1_hash (sha1_hash) VALUES ';
for ($i=1; $i<1000001; $i++) {
 $query .= "('".sha1(genRandomString(8))."'),";
    $count++;
    if ($count ==10000) {
    //result
 $result = mysql_query(rtrim($query,',')) or die ('Query error:'.mysql_error());
    if ($result) mysql_free_result($result);
    $count = 0;
    }
}

$time_end = microtime(true);
echo '<br/>'. ($time_end - $time_start);
}

//function to generate random string
function genRandomString($length)
{
$charset='abcdefghijklmnopqrstuvwxyz0123456789';
$count = strlen($charset);
 while ($length--) {
  $str .= $charset[mt_rand(0, $count-1)];
 }
return $str;
}

EDIT: The $time_start and $time_end variables are ONLY for performance testing purposes. Also the MySQL table has two fields only: ID int(11) UNSIGNED NOT NULL AUTO_INCREMENT and sha1_hash varchar(48) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, the engine is MyISAM
EDIT2: The computer hardware point of view is not related to the question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T18:18:43+00:00Added an answer on June 3, 2026 at 6:18 pm

    Inserts are generally done in large batches because indexes are updated after each insert. Batching allows you to insert many records then update the indexes only once at the end instead of after each row.

    However, in the case of an auto-incrementing primary key index, the index has to be extended in order to even add the new row, so you’re not saving anything there since you don’t have any other indexes.

    Batching also saves on some overhead in the parsing of the queries and locking. However, you might also consider using parameterized queries (PDO).

    Inserting one record at a time using PDO’s parameterized query would also be very fast, since MySQL only has to parse the query once, and from then on, it uses a low overhead binary transfer of the row data.

    You might lock the table before the insertion begins with LOCK TABLES. This will save a little bit in table lock overhead.

    Also, since SHA1 will always be 40 character hex encoded ASCII value, you should consider using CHAR(40) instead of VARCHAR(). This will speed things up as well. Also, if the SHA1 column is indexed, use a single-byte character set instead of UTF8 to reduce the size of the index and speed things up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've added a field to a MySQL table. I need to populate the new
I am trying to populate a selection box (dropdown) from a mysql table in
Actually I need to populate MySQL database from a SQL file which was generated
I am trying to load a data file into mysql table using LOAD DATA
I need to populate a currently empty table with a hundred or so fake
Ok, so I need to populate a MS Access database table with results from
I have a very big table of measurement data in MySQL and I need
I'm trying to populate a table structure with around 5 fields of data from
I have a table in MySQL populated as follows. Now I need to select
I have a huge table in my database (MySQL)with millions of data. I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.