Using TOP, Limit while fetching data using ID from database [closed] - mysql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I read somewhere on Internet that using TOP(MSSQL) or LIMIT(MySQL) in your query is best practice.
He explained: If database has millions record and if you use limit then database algorithm will stop filtering other data when it gets record you requested in LIMIT or TOP.
My question is when you fetch record using WHERE condition with ID, So LIMIT or TOP does make any difference as there'll be unique id in database.
PK is applied on that column
SELECT TOP 1 *
FROM TABLE_MASTER
WHERE ID = 10`
OR
SELECT *
FROM TABLE_MASTER
WHERE ID = 10 LIMIT 1`
If this question already asked Please give me link as I was unable to find stackoverflow thread.

If you have a WHERE clause picking a specific row by a unique id, then the query is already restricted. It will scan only the single row matching the specific value. There is no benefit to using TOP or LIMIT in this case.
If someone says to you, "feature X is best practice" that doesn't mean you should use feature X even when it makes no difference.
Using TOP or LIMIT is useful if you have no condition in the WHERE clause, or a condition that would match a very large number of rows. Instead of returning thousands (or even millions) of rows you don't need, you can restrict the quantity of rows.

If there is any chance that ID is not unique there is the possibility that more than one record could be found, having no LIMIT or TOP statement could mess up your code if you only expect one record. As such, it usually doesn't hurt to put the LIMIT / TOP statement in there just in case. If the ID is already a unique PK it won't make any difference on an efficiently coded database engine (aka pretty much all of them).

Related

How to design "like on posts" in relational databases? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 11 months ago.
Improve this question
I'm developing a kind of social app, where an user can write a post, and give like on other posts.
And on the frontend users can see how many likes a post has.
In this case, how can I handle the count of likes for each post?
What I've been thinking so far is...
(1) maintain the post_likes table whose primary keys are user_id and post_id. Everytime users give like on a post, a new row is appended on the table. The count of likes are calculated from counting the corresponding rows of post_likes table.
(2) posts table has a like_count column. Everytime users give like on a post, the value of the column increases by 1. The count of likes are calculated from selecting like_count value.
I think that the implementation (1) would be better in that which user gave like to which post. However, it seems that it could be inefficient since select COUNT(*) should be executed in order to get the like count whenever users requests a page.
what is the best approach in this case?
Both approaches have their pros and cons, but I think approach (1) is a better option for several reasons:
You can prevent users from liking their own posts (if you want to implement that).
You can allow users to remove their like.
You can prevent one user liking a post multiple times.
If performance is an issue you can opt for a new table post_likes_count that will periodically be updated from post_likes with the new like count for a post. Again, keep in mind it depends on what you specifically want to accomplish.

Cursor Based Pagination Across Multiple Tables [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have 3 tables that are accessed as individual feeds and also a group feed.
For individual feeds, I can implement cursor-based pagination based on each row's unique id.
How would I implement cursor-based pagination for the group feed, which basically combines all 3 tables into 1 feed?
Each table has unique ids and a timestamp for when it was created (although this is not unique).
I've considered using the timestamp as some sort of pointer, for example, results after a particular timestamp, but this could lead to missing results, as if you requested 10 rows, after a timestamp, and these rows all had the same timestamp, as did another 20 rows, when you perform a subsequent request, you will miss those following 20 rows.
How can this problem be tackled?
Window functions.
MySQL 8.0 introduced support for standard SQL window functions. See https://dev.mysql.com/doc/refman/8.0/en/window-functions.html
SELECT *
FROM (
SELECT ..., ROW_NUMBER() OVER () AS rownum
FROM <multiple tables joined>
) AS t
WHERE rownum BETWEEN ? and ?
No need for LIMIT. You just use parameters to select the range of rows corresponding to the current "page" you want to view.
If you answer "but I haven't upgraded to MySQL 8.0 yet," then I would say now you have a good reason to upgrade.

how to get last updated column name from mysql [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Is it possible to get a particular updated column name from a record?
if so, please help me. since i am creating a history table to store a updated column and their values from rest of the tables.
Please suggest me a correct way to deal this....
I believe it is not possible.
However, you can create an update trigger which monitors the table's columns and have that trigger insert records in your history table. Hope this helps.
The only way for doing this is Using something like a Log file, in this case you will create a table that will contains the updated columns each time there is an update.
In this way you will be able to get the last record that will contains the table_name and the column_name.
And you can use a query looks like that:
select id, column_name, table_name
from Log_table
order by id desc
limit 1

Using SELECT * FROM table [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I understand that using a SELECT * FROM table is considered a bad practice. One reason behind this is that you are select ALL columns from the table , and in case a the table is modified in future and there are extra columns added, you will be fetching those too, and perhaps will not need those.
Assuming that at the time of writing the SELECT * , first time, there indeed was a need to select all columns.
My Question is : Is there any other reason why it might be a bad practice ?
First thing that comes to my mind is the projection the Wikipedia covers that part pretty nice. And in DBA website you have a nice question about it.
You not only miss control over the columns you will present, you also miss the control over the order of the columns. By specifying them you get to have the full control over the resultset + you optimize its size by the elements you really need.
To answer your question, you also miss the possibility to control the location of each column.
SELECT * FROM table; # it would depend of the table internal order
['Surname','Office','Name','Irrelevant']
SELECT Name,Surname,Office FROM table; # you decide which column to show the element
['Name','Surname','Office']
Another reason could be the bandwith consumed : if you only need one or two informations, it is useless to get all the informations.
The application which receives the data is also slower because it has to get a large amount of data whereas it only need a few.

Role of where clause in query using exist condition? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am learning sql at the moment and i come across exist condition. What i know clearly is that and exist checks for the result of the sub query and if the result is TRUE outer query runs and if it is false outer query does not run.
But i am unable to understand is that how where clause fits in all this and how does it relates to outer query.
Please also explains to me what exactly happens after exist condition checks to TRUE.
Here is an example on how EXISTS relates with the outside query:
SELECT *
FROM classes a
WHERE EXISTS (
SELECT 1
FROM student b
WHERE b.a_id = a.id -- Here you relate inner query with outside query
)
You want to get the records in classes where there are at least one record in table students with a foreign key of the id in table classes.
You relate them in the WHERE of the inner query.
If there are no students in that class, the outside query will not return that class.