简体   繁体   中英

How to transfer a multidimensional array to a MySQL table (through Zeoslib)?

I am using the Zeoslib library in Delphi.

I have a large multidimensional static array that I need to transfer to an empty table in a local MySQL database. How can I do this efficiently?

Just iterate through a million insert statements?

  1. You can use MySQL syntax: INSERT INTO tab VALUES (v11,..., v1n), ..., (vm1, ..., vmn) . IOW, you can collect your array rows into chunks consisting of M rows. This will seriously improve performance. ( More )
  2. You can offload array into text file, then use LOAD DATA INFILE statement to load text file efficiently. ( More )
  3. You can consider to use other 3d party libraries, like AnyDAC. Which implements Array DML feature, which is intended exactly for your task. ( More )

Multi-dimensional array's don't translate well to MySQL.

If you're dealing with a tiny array you'll probably get things done, but it just doesn't scale. No matter what, it's going to get ugly real soon.

  • For a simple 2-dimensional array, you could consider creating a column for one dimension, and use rows for the other. However, you cannot have more than 4096 columns in a table:

http://dev.mysql.com/doc/refman/4.1/en/column-count-limit.html

There is a hard limit of 4096 columns per table, but the effective maximum may be less for a given table. The exact limit depends on several interacting factors, listed in the following discussion.

Every table has a maximum row size of 65,535 bytes. This maximum applies to all storage engines, but a given engine might have additional constraints that result in a lower effective maximum row size.

  • Or, you can create a row per array item, and store the index of each dimension as a primary key.But when you want to retrieve the values, you need to create a monster query.
  • You either need to do:
    • multiple joins, but you can only do 61 joins in a query:

http://dev.mysql.com/doc/refman/5.0/en/joins-limits.html

The maximum number of tables that can be referenced in a single join is 61. This also applies to the number of tables that can be referenced in the definition of a view.

    • Or create a query, group by the row key, and conditionally pick the right value with the combination of a group function and a condition, something like this: sum(if(x=1,y,0))

I had the same problem, only in php 2D arrays. Save the dimensions of the array (x, y, z etc. length, meaning the number of values on each level). then join the whole array into ane long string, divide with a special, unique character like | or , , and when you fetch the data you can split the sting based on the dimensions data.

If you need it, I can show you my php code, but I see you prefer delphi.

EDIT: this is an answer for your question before you edited it. Now it's kind of irrelevant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM