mysql select & count do not match, rogue script?

ghz 8months ago ⋅ 54 views

I am using php to import rows from a large .txt file to mysql on a shared server. I created the structure of mysql in advance to match the .txt format and added an id with auto increment.

The mysql file was updated. However, the program timed out and the results are troubling.

Using phpMyAdmin:

  1. the last id is 161,867.
  2. “SELECT COUNT(*) FROM my_sql_file” is 161,867.
  3. “SELECT * FROM my_sql_file” is 155,737.

Further more, after checking these results several times over the course of several hours, results #1 & 2 remained at their original number. However, result #3 kept changing. Sometimes the total would be higher, sometimes lower.

Tech support suspected a rogue script updating the file. I did not sanitize the data before writing to mysql. Could a script in the data be the problem? Is there any other explanation?

No errors were in the error log. The results were different by 10s of 1000s, and continued to change after the process ended.

Answers

It sounds like you're facing a data integrity issue during the import process, where the number of rows in your MySQL table doesn't match the actual data. There are several potential reasons for this discrepancy:

  1. Data Integrity: Since you mentioned not sanitizing the data before importing, it's possible that there are unexpected characters or formatting issues in the text file that are causing the import process to fail or skip certain rows. This could result in a mismatch between the number of rows imported and the total count in the table.

  2. Import Script: There might be issues with your PHP import script, such as timeouts or memory limits, causing it to not import all the rows from the text file successfully. This could lead to an incomplete import and the mismatch in row counts.

  3. Concurrent Access: If there are other processes or scripts accessing or modifying the MySQL table concurrently with your import process, it could result in inconsistencies in the row count. This could explain why the row count fluctuates even after the import process has completed.

  4. Data Modification: It's also possible that there are scripts or processes modifying the data in the MySQL table after the import process completes. This could include updates, deletions, or additions to the data that are not accounted for in your import process.

To troubleshoot the issue, you can:

  • Sanitize Data: Ensure that your data is properly sanitized before importing it into MySQL to prevent any unexpected issues with the data causing import failures.
  • Check Import Script: Review your PHP import script to ensure it's handling errors properly, not timing out, and importing all rows from the text file.
  • Monitor Concurrent Access: Check for any other processes or scripts that might be accessing or modifying the MySQL table during or after the import process.
  • Logging and Monitoring: Implement logging and monitoring in your import process to track any errors or inconsistencies and help identify the source of the issue.

By investigating these potential causes and implementing appropriate measures, you should be able to identify and resolve the discrepancy in row counts in your MySQL table.