I am working on an image processing project. I am using php for the GUI and matlab for the algorithm and Mysql as my database. I have 30,000 of images stored in the database for now. My matlab program will generate 3 arrays for each image containing 300 elements. So, my question is
Whether to save that arrays generated from matlab for all images in a single txt file or create a txt file for each image. Which method will be easier to retrieve datas and store into the database?
How hard it is to copy array form a txt file and saving it into the database? Is there any standard process for this?
The elements of the array must be retrieved for further computation. Can we use serialize and deserialize for this purpose?
I have to compare 2 array elements at a time and obtain a third array with the minimum values from both array. Eg A=[1 2 3 4] and B=[6 1 4 2] I have to compare each elements of this 2 array and generate a third array c=[1 1 3 2] that is comparing each elements of the array with its corresponding elements of the other array and storing the minimum element in the third array. This process is repeated with thousands of arrays comparing with 1 fixed array. Is there any php function to do this?
Any suggestions and help will be highly appreciable. Thank you.
The best solution in this case is to create a separate meta table that stores the data(arrays) that relates to your images.
Here's a simple example EER:
The combination of image_id (referencing foreign key), array, and index make up the primary key for the meta table.
I'm assuming you can safely represent the keys of your arrays as just 0-indexed, and all values of your arrays are also numbers (which is why all fields are of type INT, but if not, adjust the datatypes accordingly).
image_id represents the linked image_id.
array represents the specific array (you said up to 3 arrays, so values in this column would be anywhere from 1-3).
index is the numerical index in the array.
value is the value in the array paired with the index.
Example data in your meta table might look like:
image_id | array | index | value
------------------------------------------------------
256 | 1 | 0 | 5
256 | 1 | 1 | 9
256 | 1 | 2 | 4
256 | 1 | 3 | 23
256 | 1 | 4 | 1
256 | 2 | 0 | 9
256 | 2 | 1 | 15
256 | 2 | 2 | 8
256 | 2 | 3 | 19
256 | 2 | 4 | 11
In the above example data, we have two arrays (each represented by 1 and 2 in the array column) with 5 elements each (key represented in the index column, and value represented in the value column.
You can store however many arrays you want, with however many indexes you want.
You can also perform your needed array comparison calculation right in MySQL using GROUP BY. Here is how you can find the minimum value for each key across all arrays for image_id 256 (let's say there are 3 arrays):
SELECT index, MIN(value) AS minvalue
FROM image_meta
WHERE image_id = 256
GROUP BY index
Since a composite index is set up on (image_id, array, index), this should be extremely quick.
This design also allows you to have a variable number of indexes and arrays per image.
Reading through your problems, regardless of my comment, a relational database would be better for you, and if performance is not a big issue, then SQL would be the language to solve your issues best, not on the PHP level.
And yes, PHP has easy ways also to solve those problems, albeit not as easy as in SQL.
Related
i´m new here and have a project where i have a performance problem that seems hard to fix. I have created a search for objects that have availibilities means a very simple structure:
ObjectID | Date | Number of available objects
---------------------------------------------
Object1 | 01.01.2019 | 1
Object1 | 02.01.2019 | 1
Object1 | 03.01.2019 | 0
Object1 | 04.01.2019 | 1
Object1 | 05.01.2019 | 1
Object2 | 01.01.2019 | 1
Object2 | 02.01.2019 | 1
Object2 | 03.01.2019 | 0
Object2 | 04.01.2019 | 1
Object2 | 05.01.2019 | 1
I´m working with mysql and php
A typical query would be:
Which objects are available between 01.01.2019 - 28.02.2019 10 days available in a row.
It´s not really hard to make it working with mysql but once you have more then 10 users using the searchfunction the server load becomes extremly high eventough the table is optimised (indexes etc.) The server has 2 cores with 4 GB of RAM.
I also tried to store the dates comma separated per object in a table and let the application search but that creates extrem high traffic between application and database which is also not a real solution.
In total we have around 20.000 Objects and availabilities stored for max. 500 days so we have around 10.000.000 datasets in my first solution.
Does anybody have and idea what´s the most efficient way toDo this ?
(How to store it to make search fast ?)
For this project i sadly can not cache the searches.
Thanks for you help and Kind Regards, Christoph
Don't store dates in 28.02.2019 format. Flip it over, then use a DATE datatype in the table. Please provide SHOW CREATE TABLE.
What is your algorithm for searching?
The header says "number of objects", yet the values seem to be only 0 or 1, as if it is a boolean flag??
What is the maximum timespan? (If under 64, there are bit-oriented tricks we could play.)
By looking at adjacent rows (cf LAG(), if using MySQL 8.0), decide when an object changes state. Save those dates.
From that, it is one more hop to get "how many consecutive days" starting at one of those dates. This will be a simple query, and very fast if you have a suitable composite index.
I send a lot of request during a second to the database in a following pattern:
name1 | name2 | time
aaa bbb 5
aaa bbb 2
ccc ddd 3
name1-name2 is a composite key, there are maybe 10 combinations of those, and more than a million records in a table.
Now, because these are strings, it's really slow to select something from that. I've been thinking about converting this composite key to some integer to speed it up, and make it one column instead of two.
Will this really speed up my database?
How can I convert this composite key to achieve this?
i have a table in which a row contains following data. So i need to compare data among themselves and show which data has maximum count.for ex. my table has following fruits name. So i need to compare these fruits among themselves and show max fruit count first.
s.no | field1 |
1 |apple,orange,pineapple |
2 |apple,pineapple,strawberry,grapes|
3 |apple,grapes, |
4 |orange,mango |
i.e apple comes first,grapes second,pineapple third and so on. and these datas are entered dynamically, so whatever the values is entered dynamically it needs to compare among themselves and get max count
Great question.
This is a classical bad outcome of not having the data normalized.
I recommend you to read about Database Normalization, normalize your tables and see after that how easy it is to do this with simple SQL queries
If you need to run queries on column field 1, then why not consider normalization ? Otherwise it might keep on getting complex and dirty in future.
Your current table will look like this (for serianl number 1 only), Pk can be an autoincrement primary key.
Pk | s.no |fruitId|
1 | 1 |1 |
2 | 1 |2 |
3 | 1 |3 |
Your New Table of Fruits
PK |fruitName |
1 |Apple |
2 |Orange |
3 |Pineapple |
This also helps you to avoid redundancy.
Quick solution would be counting the amount of fruits where you insert/update the row and add a fruitCount column. You can then use this column to order by.
Zohaib has to correct solution though - if you have the time and possibility for such changes. And I definitely suggest you to read Tudor's link!
What issues are associated with maintaining multiple tress within a single table?
The motivation for having multiple trees is to avoid excessive updates to all nodes when inserting a node at the start. Each of the trees are completely separate entities.
Example Table:
tree_id | id | lft | rgt | parent_id | various fields . . .
---------------------------------------------------------------------
1 | 1 | 1 | 4 | NULL | ...
1 | 2 | 2 | 3 | 1 | ...
2 | 3 | 1 | 4 | NULL | ...
2 | 4 | 2 | 3 | 3 | ...
It's very common to store multiple trees in one table, just have to make sure the values that comprise a tree are stored correctly, otherwise it'll lead to data integrity issues like nonsensical tree constructions.
Suppose we have a binary tree, (like the one in your example). If a tree was 5 depth. ((2^n)-1) = (2^5 - 1) nodes would exist or 31 rows in the database which is trivial. Even at 10 depth it's still a small amount of rows, but would be a rather ginormous tree. And so having multiple trees, X, in there would be X((2^n)-1) = rows... in the database which isn't bad. So potentially a hundred trees could exist in one table and would only be 100k rows which is relatively small.
Additionally, suppose every new tree constructed was stored in its own table, then very quickly, the database would be filled with quite a bit of tables over time to match the number of trees that exist. And it just seems like not a good idea to make extra tables that are unneeded, adds unneeded complexity in the code side to have to access these multiple tables.
Looking at your table in detail, it doesn't quite look right in terms of columns, but I'm sure that table example is just something thrown up quickly to show us what you mean.
tree_id, node_id, left_node_id, right_node_id, various_fields...
Um, be sure to index those _id fields.
Hey,
I'm having a sorting issue. I have the rows:
32
16
8
semifinals
finals
that i need to be sorted like that. The problem is they don't always appear in that order. Right now I'm using: ORDER BY ABS(roundOf) DESC and it's comming out:
32
16
8
finals
semifinals
Thanks.
There is no way to do this without storing an order number with that data. The words semifinals and finals do not hold any order data comparable to the numbers of course. Store this in an array in the order you need it or if it's stored in a relational db, use an order column.
Possible db table:
order | name | numberOfPlayers
1 | finals | 2
2 | semis | 4
...
Array:
$rounds = array[1] = 'finals';
etc.
rename your finals and semifinals into numbers.