How to create some kind of "dynamic index" in the database

Asked

Viewed 64 times

0

I have a table with 10000 records, but here I will represent with only 10 of them.

+-------+---------+------------------+
| index | posicao |      nome        |
+-------+---------+------------------+
|     1 |       0 |    arquivo 1.mp3 |
|     2 |       1 |    arquivo 2.mp3 |
|     3 |       2 |    arquivo 3.mp3 |
|     4 |       3 |    arquivo 4.mp3 |
|     5 |       4 |    arquivo 5.mp3 |
|     6 |       5 |    arquivo 6.mp3 |
|     7 |       6 |    arquivo 7.mp3 |
|     8 |       7 |    arquivo 8.mp3 |
|     9 |       8 |    arquivo 9.mp3 |
+-------+---------+------------------+

But when adding a record in the middle there, I need the whole table to update to understand your new posições.

+-------+---------+-------------------+
| index | posicao |       nome        |
+-------+---------+-------------------+
|     1 |       0 |    arquivo 1.mp3  |
|     2 |       1 |    arquivo 2.mp3  |
|     3 |       2 |    arquivo 3.mp3  |
|    10 |       3 |    arquivo 70.mp3 | < --- novo registro
|     4 |       4 |    arquivo 4.mp3  |
|     5 |       5 |    arquivo 5.mp3  |
|     6 |       6 |    arquivo 6.mp3  |
|     7 |       7 |    arquivo 7.mp3  |
|     8 |       8 |    arquivo 8.mp3  |
|     9 |       9 |    arquivo 9.mp3  |
+-------+---------+-------------------+

You can see that the arquivo 4.mp3 that before was in the posição 3, now switched to the posição 4, and all in front of you, too. The problem is that updating thousands of records not feasible for me, as I could make each record knew its position without having to update each one individually ?

  • Take a look at Elastic Search https://www.elastic.co/pt/products/elastic-stack

  • "how could I make sure that each record knew its proper position without having to update each one individually, "for it is, how could he know his proper position without you informing? See your example, why the record has to be in position 3 and not in the seventh (if it is a simple classification by name) or tenth (if it is the last one inserted or the one sorted by the file number)?

  • Have any answers solved what was in doubt? Do you need something else to be improved? Do you think it is possible to accept it now?

2 answers

3

No need and no such thing as "dynamic index", all indexes are dynamic by nature, they are updated when you have information that affects you.

What you need to do is change the table, the way you described it, there is no miracle, there is a law of physics that prevents things from changing on their own. You could even have some resource in the database that you would do alone, but the updates in the table would occur in the same way, and precisely because there would be no real gain and would provoke an illusion, it is good that there is something like this. If your problem is changing the data you will have to change the data.

Why is it impossible to change thousands of lines? It says you have a table with 10,000 lines, this is one of the smallest tables I’ve ever seen, especially if you only have those 3 columns. Even to find the data will do almost the same thing you have to do to change this data, of course writing costs more, but it is not the end of the world.

Michel Simões' answer gives the SQL that you would probably already do and changes thousands of lines, IE, it does not really answer what you asked.

Without understanding the exact problem nor can we offer an alternative solution, we do not even know why there is a column index and a posicao, maybe I shouldn’t even have both, yes, to have one id stable, but we don’t know.

If you can rephrase this could have a column posicao and, I don’t know, a column desempate, then you don’t touch posicao and by default the desempate always enters as value 0, And if you enter one right after it 1, and if there’s another one after that posicao remains the same and the desempate enters as 2, and so on.

Of course, if you have too much of that, ultimately, you could end up having them all posicao equal and the desempate turn what was the posicao, but then you can have a routine that normalizes that and when you spin it it will redo only once renumbering all posicao and zeroing out the desempate, thus resolves.

If you have to enter in the middle of this tie-breaker list you will have to change the values of desempate, but, if it doesn’t happen what I said in the previous paragraph, it will be a much smaller amount of lines to change and the normalization of positions that I quoted will make it not necessary, it is one of the most famous techniques of computing, the divide to conquer.

But this is too complicated and would only be interesting if this insertion occurs several times a second, which I doubt. Make it simple, I doubt there’s a real problem there, there’s just something you don’t want to do because you think it’s bad, without any foundation. Otherwise the question does not give enough information to help you better. If you do not know how to do it right it will be worse than the update pure and simple.

If you have a real performance problem I would do a series of alternative tests to solve, has a plethora of possibilities, but for that you need to have two characteristics: understand very well the functioning of a database and general aspects of computing, including mathematical aspects, something that few people want to study; and at the same time understand very well the problem in detail. I don’t know about one of them (and if you knew it would take hours or days to come up with a result, which may even be that the best way is what you’re already doing) and you don’t know the other, so it’s hard to find a solution.

0

Hello, Lucas, hello, Lucas! I tried to reproduce the problem and it really is more complex than it actually looks. An alternative to your problem would be to simplify SQL... This way, Voce does what you would like to form at least 'elegant''

update TABLE set order = order + 1 Where order > 3 (to add 1 for the items above the new order)

and so position 3 would be free for you to enter record 10 and all would go up 1.

  • Can make an example table ?

  • With you, just let me have a minute here today that I do for you. Good luck there man!!

  • @Lucascarezia gives a look at the answer, if it suits you...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.