don’t use “offset” in your SQL

Поділитися
Вставка
  • Опубліковано 26 лис 2024

КОМЕНТАРІ • 217

  • @hnasr
    @hnasr  2 роки тому +4

    Learn the fundamentals of database engineering with me, get my udemy course over 19 hours worth of database engineering first principles. Head to database.husseinnasser.com for a discount coupon, link redirects to udemy with discount coupon applied.

  • @mayankkaul3218
    @mayankkaul3218 3 роки тому +3

    I feel this is a good explanation of how an offset works. In some cases it doesn't matter as the user won't turn to the 100+ page but will use search if it exists. Plus in cases where the user jumps to some random page, this won't work. But this video got me thinking. Great work!!

  • @alexk6275
    @alexk6275 3 роки тому +5

    Sometimes you can do "spatial pagination" when working with geospatial data. Basically assign each row a geohash, iterate over (shorter) geohashes for a region of interest, use the shorter geohash to lookup a page of rows. Just have to make sure the geohashes assigned to your rows are shorter than the geohashes covering your region of interest.

  • @mhrddd
    @mhrddd 3 роки тому +25

    Basically, this is the exact way that the big platforms like Google or Facebook are using for pagination. They give you a pointer, and you can fetch the next page results by that pointer.

    • @MagicJohnsonHardCore
      @MagicJohnsonHardCore 3 роки тому +4

      FB is using Cassandra DB and that is exactly how Cassandra db pagination works. Limit per request on CassandraDB is 5000 so not possible to retrieve all data at once. For next page you pass pointer and than get another 5000 results (or any other number less than 5000 that you have set) like you mentioned.

    • @NurulIslam-dw8yp
      @NurulIslam-dw8yp 3 роки тому

      Nice

    • @im_parth
      @im_parth 3 роки тому +1

      Also Firebase's Firestore implements pagination in the exact same way !

  • @ShreyasSegu
    @ShreyasSegu 3 роки тому +95

    Awesome content, how do we optimize the paging when the IDs are UUIDs?

    • @sangomalsa
      @sangomalsa 3 роки тому +6

      Good question to raise

    • @n1amr
      @n1amr 3 роки тому +3

      I was thinking of this, maybe using creation time column for filtering

    • @ShreyasSegu
      @ShreyasSegu 3 роки тому

      But then we have to index creation time column, not to mention whether it exists or not in the first place

    • @hnasr
      @hnasr  3 роки тому +73

      That would be difficult since UUIDs are randomly generated. I would rely on another field (int or date) to perform this.
      UUIDs are great but this is one limitation that I personally ran into too

    • @ShreyasSegu
      @ShreyasSegu 3 роки тому +5

      Auto incrementing IDs are the way to go then haha

  • @saggitt
    @saggitt 3 роки тому +39

    This is a good solution when you can use it, but in many cases where-condition or sorting do not allow you to do that. Just apply filtering or ordering by any field other than id and this approach stops working.

    • @samadams7278
      @samadams7278 2 роки тому

      Why is that?

    • @davestorm6718
      @davestorm6718 2 роки тому +7

      @@samadams7278 - Because your ids can be all over the place and in no particular order when filtering by anything else

    • @alextz4307
      @alextz4307 Рік тому

      If ID is a primary key then a B-Tree Index is created on it which sorts it by definition. Therefore paginating using a where clause is optimal.

  • @sajal_sarwar
    @sajal_sarwar 3 роки тому +6

    I just feel lucky to stumble upon your channel. : )

  • @lostbird3791
    @lostbird3791 3 роки тому +1

    Omg! Thank you for this. I’ve been using this since like forever now, I never knew it’s common and it has bigger effects like this but I was feeling comfort in using this with my infinite scrolling lists so later on used to do the same with traditional pagination. But glad it did better.

  • @XEQTIONRZ
    @XEQTIONRZ 3 роки тому +10

    Whoa.. I totally didn't know that.
    Btw, Happy New Year brother.

    • @hnasr
      @hnasr  3 роки тому +3

      Happy New year!!

  • @osamaa.h.altameemi5592
    @osamaa.h.altameemi5592 3 роки тому +29

    What if the user clicks directly on page 7? we don't have the id of page 6 nor 5, any solution for that?

    • @hnasr
      @hnasr  3 роки тому +27

      Correct no longer can you jump to a specific page without a hint of an additional filter (id or date) but personally I would rather disable that feature and gain performance by letting the user scroll pages incrementally. I don’t recall me clicking on anything in the second page or third page.
      If that feature is a must have I might add another table that maps page to id maybe?
      I dont know I would like to see what others think this is interesting 🧐

    • @lighty8561
      @lighty8561 3 роки тому +3

      # page 7 (10 entrys per page)
      select * from customers where id > (7 - 1) * 10 and id

    • @osamaa.h.altameemi5592
      @osamaa.h.altameemi5592 3 роки тому +5

      @@lighty8561 i see what you are saying but ur code assume we have results in each and every row of the the previous ids which might be the case.
      take the following case
      i have results in ids
      1 to 10:
      Then nothing till id-100
      now the equation you provided doesn't hold true. Thank you for the answer buddy.

    • @osamaa.h.altameemi5592
      @osamaa.h.altameemi5592 3 роки тому +1

      @@BernardoBits if you go with the offset then you are doing the bulk of the work assume we want to go for rows 1 million to one million and ten.
      Thank you buddy but I think there are too many edge cases for this one.

    • @mileusna
      @mileusna 3 роки тому +4

      But what if Google throws the user directly to your site on page 10 site_com/news/10? Or what if “user” is Googlebot who crawls page 7 directly? Lots of real-world scenarios out there where you have to offer pages to search engines to crawle your content etc.

  • @akashchoudhary8162
    @akashchoudhary8162 3 роки тому +18

    But say someone has to jump to that page 100 directly without having seen any posts before. What id number would you choose then, because sometimee the ids in the middle would be missing if someone deleted their posts? If a lot of people started deleting their posts wouldn't this cause an issue?

    • @AltianoGerung
      @AltianoGerung 3 роки тому +10

      Interesting,
      I think the explained solution is best for comments/infinite scrolling UX where the user can't decide which page to look into

    • @RajDhana586
      @RajDhana586 3 роки тому +4

      The explained approach is not suitable for directly jumping to a particular page. It works well for infinite scroll OR just providing 'next' button. Also in practice we just don't use id alone for ordering, we use multiple columns in such cases where clause becomes complex as the number of order columns increase.

    • @MyBerthony
      @MyBerthony 3 роки тому

      For autoincremented id you can do something like this: Multiply the page number by the limit value + the id of the first id in the database minus 1.
      ?page=1777&per_page=10
      Let s assume the first id in the db is 10000
      Your new id for the where clause is:
      10000 + (1777 x 10) - 1
      First value in the db can be replaced by starting point of auto_increment

    • @MrDimitri71
      @MrDimitri71 3 роки тому +2

      @@MyBerthony This does not work if you delete more than 10 items from the db for instance. I imagine that you would have to have a column that you would edit everytime something was deleted, or there could be another function that returns to the client app the ids, so the client can now see what ids are in witch page.

  • @joaquinel
    @joaquinel 3 роки тому

    GREAT. thanks for the warning. Performance AND integrity.
    workaround... depends on the context. size, latency, concurrency, the index you can work with...
    Old times I used to solve with temporary non normalized tables on the fly,
    performance and integrity was far better, but I didn't face the crazy sizes of modern databases.

  • @wepranaga
    @wepranaga 3 роки тому +9

    I guess by that definition, I should make a field in the table that's indexed for pagination use (like created_at, etc)

    • @vzool
      @vzool 3 роки тому

      Maybe, I think id field is a better choice, if you use it with Hashids

    • @wepranaga
      @wepranaga 3 роки тому

      there's always hackier way to do things :)

  • @frikishaan
    @frikishaan 7 місяців тому +1

    Please correct me if I am wrong The order by column in this case will need an index to be performant. So this will not be a good idea in case the user has option to sort by many fields like in a back office application.

  • @rapha5210
    @rapha5210 3 роки тому +12

    Not very useful for grids than can be sorted by any column desc/asc

    • @trungthanhbp
      @trungthanhbp 3 роки тому

      same here, do we have any solution for this?

    • @brucepierson9941
      @brucepierson9941 3 роки тому +1

      Incorporate the search and sort into the paging query. Your users will rarely have to go beyond a few pages.

  • @hoelefouk
    @hoelefouk 2 роки тому +1

    Learning so much from you, thank you Hussein!

  • @jigarwala8424
    @jigarwala8424 3 роки тому +1

    for anyone looking up on Google for this technique, its called keyset pagination

  • @MartinPHellwig
    @MartinPHellwig 3 роки тому +1

    Yes, however this only works if the column you offset from has values that are both unique and serial sorted by the sorting parameters. If that condition is not true then you still have to use offset or create a column that has that property, which makes inserting a new row a job that requires first fetching the two values where the new row has to be in between and calculate the average value between those sorting values.

    • @npc73x
      @npc73x 2 роки тому

      Yes, this is also problematic when client requires rows from 100 to 200, but if the rows 150 - 170 has been deleted, we have only 80 rows but the client expecting 100 rows, so we make additional SQL queries, to get required rows, when we need do that, we also want to inform the client, In next request you need to ask from this id, example 230. kind of expensive but much better than offset may be.

  • @DarkKnight-ree
    @DarkKnight-ree 3 роки тому

    My eyes are opened, thanks man this means alot.

    • @hnasr
      @hnasr  3 роки тому

      Glad I could help (L) thanks

  • @ezequielfalcon4869
    @ezequielfalcon4869 3 роки тому +5

    What would be the best approach if we have a paginated grid with different columns and you can sort by any of them? Love your videos btw.

    • @ricko13
      @ricko13 2 роки тому

      you cannot

  • @marslogics
    @marslogics 3 роки тому +6

    Great content as always.
    I guess you could also provide the use cases, like infinity scroll and where user is going page by page only.
    As if user is allowed to jump pages then how come system know which id forward it should move...

    • @optimumcool
      @optimumcool Рік тому +1

      the client needs to know the last post it fetched, and you then paginate using that page...
      when scroll hits 1400
      var id = items[100].id
      callPaginate(id)

    • @marslogics
      @marslogics Рік тому

      @@optimumcool Thank you for clarification.

  • @dasten123
    @dasten123 3 роки тому +5

    wow I didn't know offset was this inefficient :O
    But the alternative doesn't work for queries with special filtering and ordering ;(

    • @DylanYoung
      @DylanYoung 3 роки тому

      Yes it does. Any filters should be fine. To change the ordering you have to change the "next page hint" to the new ordering fields rather than id.

  • @mahipaljyani
    @mahipaljyani 3 роки тому +1

    Awesome content, loved the way you explained the things 😊

    • @hnasr
      @hnasr  3 роки тому

      ❤️❤️❤️

  • @BR-lx7py
    @BR-lx7py 3 роки тому +3

    Why does "offset" work this way though? Seems like a pretty simple optimization that if you can determine the rows that "offset" and "limit" specify only from an index, then you don't fetch the rows you wouldn't return.

    • @2547techno
      @2547techno 3 місяці тому

      3 years later but the short answer is that the data on disk for the entire table is not necessarily stored contiguously, therefore you cannot simply jump to an offset in the database file. For very specialized database systems this may be possible but for the majority it is not

  • @ahmadsaeed2420
    @ahmadsaeed2420 3 роки тому +3

    I'm asking a question that might show up my inexperience with databases.
    How we handle skipping the rows using index loop when we are also filtering records on different fields. Consider an api endpoint for search that returns paginated results

    • @ahmadsaeed2420
      @ahmadsaeed2420 3 роки тому +1

      I'm considering that we use auto-increment ids as PK/index

    • @RajDhana586
      @RajDhana586 3 роки тому

      @@ahmadsaeed2420 What happens some records/rows are deleted. I think this technique may not be suitable if we want to directly jump to some page number. It works well for infinite scroll OR just providing 'next' button. Also in practice we just don't use id alone for ordering, we use multiple columns in such cases where clause becomes complex as the number of order columns increase.

  • @smalldog1
    @smalldog1 2 роки тому

    Nice video man, clear and simple.

  • @GoddamnAxl
    @GoddamnAxl 3 роки тому +1

    What I don't understand is that why db makers don't optimize offset so that when used without a where clause, it scans past the offset part and fetch only the limit part from disk

  • @satwikburman6841
    @satwikburman6841 8 днів тому

    At application level we need to right an inner query to fetch the last ID and then use it in the outer query.

  • @Peter-bg1ku
    @Peter-bg1ku 3 роки тому +7

    This won't work if you order your records with a field other than the auto-increment key. Users ordered by first name for example

    • @RajDhana586
      @RajDhana586 3 роки тому +1

      We need to use the same columns which are used to order our results. If we have ANY kind of ordering this technique can be used.

    • @rahulsangvikar7973
      @rahulsangvikar7973 3 роки тому

      You should not use first-name/any other useful column as a primary key, it violates the Single Responsibility Principle. You can create an index (unique or not) on that column but it is recommended that you keep primary key as auto-increment.

    • @Peter-bg1ku
      @Peter-bg1ku 3 роки тому

      @@rahulsangvikar7973 I didn't mention anything regarding using first name as a primary key. My comment points out potential issues caused by ordering using a non increment field.

    • @rahulsangvikar7973
      @rahulsangvikar7973 3 роки тому

      @@Peter-bg1ku My bad, I misunderstood, but in that case I agree with @Dhanaraj D

  • @renon3359
    @renon3359 3 роки тому

    Damn! Totally did not know this. Thank you brother.

  • @MrYokyScape
    @MrYokyScape 3 роки тому

    Great video! Definitely going to be using this.

  • @hoxorious
    @hoxorious 3 роки тому

    Excellent video. Also please make a video about Cassandra. Thanks 👍

  • @ArjenHaayman
    @ArjenHaayman 3 роки тому +4

    I do wonder how you would implement a pager on a website where a user can immediately jump to say page 12. Using this technique you'd need to know where page 11 ended, so in the end you still need to read all records to determine where each page ends?

    • @RajDhana586
      @RajDhana586 3 роки тому +2

      The explained approach is not suitable for directly jumping to a particular page. It works well for infinite scroll OR just providing 'next' button. Also in practice we just don't use id alone for ordering, we use multiple columns in such cases where clause becomes complex as the number of order columns increase.

    • @mushifalinawaz
      @mushifalinawaz 3 роки тому

      If the ids are consecutive then you can use a bit of mathematics: *last_id_of_current_page + (new_page_number * limit)*

    • @RajDhana586
      @RajDhana586 3 роки тому

      @@mushifalinawaz it would only work if there are no deletions and ordering is only based on id.

    • @mushifalinawaz
      @mushifalinawaz 3 роки тому

      @@RajDhana586 Yes. That's why I mentioned: *"If the ids are consecutive"*

    • @motalasuger
      @motalasuger 3 роки тому

      You could also do a select for only the Id column and limit pagesize * pagenumbers, and then ignore all the rows until you hit your pagesize, add that to the page index list, and then reset counter + repeat / continue.
      That would work for generating paging indexes for further pages at least, using the same method as explained in the video, tho you’d end up throwing away some of the id values, it should still be decently fast unless you add too big amount of pages or pagesize.

  • @chantapatct
    @chantapatct 3 роки тому +2

    Thanks for sharing, I was wondering if the offset is not good for pagination then, what is it created for? is there any use case for using offset?

    • @RabbitChannel66
      @RabbitChannel66 2 роки тому

      With this method you can't just open random page. This works if you go from start to finish or from finish back to the start.

  • @LearnByDoing7
    @LearnByDoing7 Рік тому

    Great video! But how do we handle the situation where the user jumps to a random page

  • @ruhnet
    @ruhnet 3 роки тому

    Wow this was very helpful. Thanks!

  • @ishmeetbindra5428
    @ishmeetbindra5428 3 роки тому

    Great video. Which tool do you use for slide creation?

  • @yoelczalas
    @yoelczalas 2 роки тому

    Amazing!!! thank you again Hussein

  • @DamjanDimitrioski
    @DamjanDimitrioski 3 роки тому +1

    Interesting I can use that where id trick for app data sync as well.

  • @kamaljeetsinghrana8441
    @kamaljeetsinghrana8441 10 місяців тому

    This solution looks great while this can have problems when a we can jump into any pages for example if i want to visit page 8 after 1st page

  • @BlurryBit
    @BlurryBit 2 роки тому

    That was very helpful!

  • @lastsaiyan9035
    @lastsaiyan9035 3 роки тому

    your content is awesome, thanks a lot

  • @naturebc
    @naturebc 2 дні тому

    This only works when the Id is the primary key and you are not sorting by anything else, which is not likely. It is very rare that you can use this in real life.

  • @kgck15
    @kgck15 3 роки тому

    Thank you for the important tip!

  • @AleksandarT10
    @AleksandarT10 2 роки тому

    When not having too many rows (billions) , i dont see offset as an issue. Even if the query takes half a second at a certain point. As others pointed out, if the user can navigate directly to page X, this wont work
    Anyway, great explanation

  • @anupamchand3690
    @anupamchand3690 3 роки тому

    Great video....can we use this for reverse (or back) pagination as well? I'm guessing We will need extra coding using and ORDER BY ...DESC ? Or is there a better way?

    • @hnasr
      @hnasr  3 роки тому

      Correct the change here is you determine where to start with an indexed column instead of using an offset

  • @amiraftab2475
    @amiraftab2475 3 роки тому

    Hey good content thanks for such helping videos!

  • @marcotinacci5718
    @marcotinacci5718 3 роки тому

    Thank you, that's a great tip!

  • @AnantKumarS
    @AnantKumarS 2 роки тому +1

    Awesome stuff

  • @Inthedark72
    @Inthedark72 3 роки тому

    How does this work when you paginate through items that are already filtered by the user. Let's say you have a list of movies that are sorted by score and filtered by certain genres and release dates. Each combination of where clauses and order statements is going to give you a different set of items across pages. In this example you use "< id" but there is no pattern or reference when the items are already subset in a given order. Is there a way in this situation?

  • @nadeemqureshi9779
    @nadeemqureshi9779 3 роки тому

    Gey Naseer good one. But paging is usually used with sorting and at that time our ids amy not be depicting the sort order that we really want. Like a sort with number of likes and pagination.

  • @anthonyfarias321
    @anthonyfarias321 2 роки тому

    Hello Hussein, This is a nice video as always. What happens if my primary id is not an autoincrement but uuid??? Is it the same thing as explained in the video? thanks so much.

    • @hnasr
      @hnasr  2 роки тому

      As long as you do an order by on the UUID field the paging should work. However the problem unlike integer primary key is UUID is random so it won’t give you the “latest” entries (news in my example) when you do an order by UUID.

    • @AnantKumarS
      @AnantKumarS 2 роки тому

      @@hnasr Wow! I wanted to know the answer to this query and was thinking that this is an old video and no one would actually respond. But Hussein, you are awesome

  • @syedsmith2151
    @syedsmith2151 3 роки тому +1

    Awesome 😎 But how can we use this technique with a where clause for pagination api's?

    • @RajDhana586
      @RajDhana586 3 роки тому +1

      In case of APIs - we can send an opaque string which encodes the last page last row column values(columns used in ordering). In the API result, we can name this opaque string as nextPage. When clients make next API call they need to pass this as a parameter. Now we can decode & get the column values and form the where clause needed.

  • @FreePal334
    @FreePal334 3 роки тому

    Nice one Hussain!
    But what if my query doing a join with another table, how i can use the id to mimic the pagination?

  • @gurucharan769
    @gurucharan769 3 роки тому +3

    What if the id is not an integer or a number?

    • @LeMustache
      @LeMustache 3 роки тому

      Why would that matter? As long as you can order by it it's fine

  • @brucepierson9941
    @brucepierson9941 3 роки тому

    However, people don't page thru 1000 pages. Usually they do a search to limit the results, then use paging. My users rarely go beyond page 2 or 3.

  • @mohamedelsayed1868
    @mohamedelsayed1868 Рік тому

    that is sequential pagination it wont work for all cases still using offset is the common approach and the only reliable approach could u utilize it to fetch 5th page content based on the first page? we can mix both case front sends last seen we use key set else we use offset thats what i think

  • @user-qr4jf4tv2x
    @user-qr4jf4tv2x Рік тому

    i think i can't do the index based pagination because before even paginating it needed the entire set to begin, while the actual pagination happens only when all the filtration and aggregation occurred

  • @victorlyan3844
    @victorlyan3844 3 роки тому

    Good stuff! But I think that this approach doesn't protect from duplicate rows reading, mentioned at the beginning of the video, as well. Correct me, if I'm wrong.

    • @marslogics
      @marslogics 3 роки тому +1

      It will protect, as you are fetching rows after given id, so you are guaranteed to get next page without duplicate rows

  • @tribacioustee2846
    @tribacioustee2846 Рік тому

    This solution won't work for every database table, or even many. What if the ID in my table isn't a fixed variable? What if it starts at different things, so something like 91100 is actually only the first variable?

  • @davidmataviejo3313
    @davidmataviejo3313 3 роки тому

    Can you explain more why SQL server would have crashed with the 100000 offset?

  • @ckravi8906
    @ckravi8906 Рік тому

    What should I do if I have only 20 records, the limit is 10, ordered by ascending, and in the second API call, I receive ID 20? How do I determine if I need to send another request, especially if the current query is still returning no data records found?

  • @olehdevua
    @olehdevua 2 роки тому

    What if I want paginate not all table, but just subset of mine "news" ?
    In such case you can go to next page with your approach, not on random page in your list of "news"

  • @TamanKodeKode
    @TamanKodeKode 3 роки тому

    How if i use uuid for primary key? This is not sequential record and can not do math for this field.

  • @SirRFI
    @SirRFI 3 роки тому

    Hello Hussein,
    We are building a service that gets data from various services, where each of them has it's own structure, so they are related only topic wise. Our structure reflects our needs and gets common ground with the others to some degree. Each provider is handled separately via API, using queue system. Some relations are in place for the product types.
    Generally speaking, how would you approach this task? I am mostly concerned about database related performance. Would you use separate database for originally imported data? Are relations good idea, how would you solve them? I imagine using UUIDs would speed up things here as well. This is open question, looking for advices. Thanks!

  • @DavidSmith-ef4eh
    @DavidSmith-ef4eh 3 роки тому

    Sounds like something the database driver should optimise, not the end user. Also, seems to be only a problem once you go into the 100 k territory. I doubt even PromHub doesn't have so many videos (at least now)

  • @sudosu
    @sudosu 3 роки тому

    I understand the main idea, but I can't see how it can work on complicated tables. For example, at my previous work, we had to move "deleted" records to another table to make the main table smaller. Or how do we work with records of different users or "hidden" records? And I do not remember the details exactly, but as I know, when we roll back the transaction, we have a chance to skip auto increment.
    here a example:
    id | user | status | data
    1 | 1 | null | "data"
    2 | 2 | null | "data"
    3 | 1 | "hidden" | "data"
    17 | 1 | null | "data"
    ...
    10000 | 1 | null | "data"
    and when user look at second page, he sees exactly 9 records from first page.
    select * from history where id > 10 and user = 1 and status is null limit 10;
    How I can see, is the only way to make it work is to make a virtual table with another row of ID with preselected rows, but it is probably costly than offset. Is there is another way?

  • @oleksiibuheria932
    @oleksiibuheria932 3 роки тому +1

    But what if user "skips" pages and doesn't know id?

    • @rahulsangvikar7973
      @rahulsangvikar7973 3 роки тому

      Users should not randomly go to any page. BE should expose appropriate filters so that user can find what he/she is looking for by max 2-3 paginated queries

    • @lancerhu8107
      @lancerhu8107 3 роки тому

      Very common behavior on forums. I go to a topic and then go to the last page

  • @adilamanat8117
    @adilamanat8117 3 роки тому

    Only useful in case of auto increment keys. Doesn't seems to be useful in case of composite primary key.

  • @lonewolfcoding5208
    @lonewolfcoding5208 2 роки тому

    we can use datetime as an id datatype for this to work

  • @sirajul-anik
    @sirajul-anik 3 роки тому +1

    doesn't it expose your primary id to the world? what if I use any uuid or something? what'd be the case?

    • @autohmae
      @autohmae 3 роки тому +2

      My guess is some companies encrypt the numeric ID so as not to expose the information directly. Just have paging links with something like '76c43r67' in the URL.

  • @isolveitgh
    @isolveitgh 3 роки тому

    Thank you for creating awesome and simple to understand content always. If you can, would you like to create a tutorial about algorithms and data structures.

  • @harshpratyush
    @harshpratyush 3 роки тому

    Id may not be the best because... there can be custom sort applied... Can be on any column other than id....

  • @sumarouno
    @sumarouno 3 роки тому

    Saved for tonight

  • @mehulbisht9708
    @mehulbisht9708 3 роки тому

    Hi Hus! How to do pagination with Cloud Firestore? (for native android app) There is an offset value that the cloud firestore query provides, and also an after() query modifier. So should offset never be used to fetch the next page? And even with using after() i think, the newly added document on top will not be considered to be added on it's own to the first page.

  • @migom6
    @migom6 3 роки тому

    more content and insights on databases

  • @trendyBeez
    @trendyBeez 3 роки тому

    What if your id is not auto increment? it's a randomly generated numbers or numbers and alphabet . How do u go about it?

  • @BinaryReader
    @BinaryReader 3 роки тому

    Interesting, but would creating a auto incrementing paging column with Btree indexing be a bit more flexible tho? Numeric identifiers are convenient but usually not the preference over uuid4 (guid),. Also why is it so horrifically slow? Rows are generally fixed size, so its weird it needs a full linear scan...why doesn't it (offset * row_size) into the record set? Does the way PG persist records interleave? Id expect some fragmented store to be slow, but partial fragmentation should still be on par using with Btree index lookup if using a simple (offset * row_size). What am i missing here?

  • @ricko13
    @ricko13 2 роки тому

    ok but what if you want to order by title or any other field

  • @professortrog7742
    @professortrog7742 3 роки тому +1

    Problematic if you want to jump to page N.

  • @nishalshettigar
    @nishalshettigar 3 роки тому

    What if we are using a database where the IDs are randomly generated, like elasticsearch. Elasticsearch does not support auto-incrementing IDs as well.

    • @RajDhana586
      @RajDhana586 3 роки тому

      If we use any columns for ordering the result, then we can use those columns in where clause. If we don't have any kind of order when presenting the result, that is not so good UX. So if we have any order, then the above technique can be used.

  • @NurulIslam-dw8yp
    @NurulIslam-dw8yp 3 роки тому

    Every page should have an exact id pointer by which i can paginate by id

  • @riten
    @riten 2 роки тому

    But then you cant jump to a page :/

  • @domemvs
    @domemvs 3 роки тому

    What if I need to filter some rows out? Let’s say I have some blog posts that I marked as deleted and I want to show the last 10 non-deleted ones?

    • @willi1978
      @willi1978 3 роки тому

      it works the same. you add where id > x and deleted_at is null limit 10

  • @luan_maik
    @luan_maik 3 роки тому

    I don't know why the user would page up to the 100k or 1 million offset

    • @hnasr
      @hnasr  3 роки тому +1

      Skipping to page x would be a use case, using offset just kills the performance in this case

    • @luan_maik
      @luan_maik 3 роки тому

      ​@@hnasr I agree, but the web interface should provide filters to the user find what him wants and the pagination be just forward or rewind. I get your point in the video, but in major case the problem would be a bad user interface. So maybe firstly should be analized why the user is paging up to 100k to find something. I really get your point in the video, my comment it's just a use case involving front and back-end.

  • @raminsadeghnasab7944
    @raminsadeghnasab7944 3 роки тому

    What if someone directly entered a URL that points to page 100. domain.com/products/?page=100
    in this case, we won't have the pointer.

    • @hnasr
      @hnasr  3 роки тому

      Correct, That would not have enough information to point us to page 100, the server should capture that and fail the call. Additional filters are required such as date or id.

  • @miqbalrofikurrahman9486
    @miqbalrofikurrahman9486 3 роки тому

    sadly it doesn't work with unsorted primary key.

    • @miqbalrofikurrahman9486
      @miqbalrofikurrahman9486 3 роки тому

      you just offering us to not using offset queries anymore, but it won't help us because it leaves some new cases.

  • @SujaiSD
    @SujaiSD 3 роки тому

    If we provide an API to the public, we can't expose the internal reference id

    • @motalasuger
      @motalasuger 3 роки тому +1

      Well you should be able to though, if something is so secret about a incrementing number then maybe something is wrong with the API or the internals themselves?
      Do you have a requirement to hide which entries were before or after each other to the public perhaps?

    • @SujaiSD
      @SujaiSD 3 роки тому

      @@motalasuger Not really, I was just mapping this with the original api that was already exposed like the page number and count per page.
      The sorting problem is a persistence layer issue, I was wondering why we should expose it to the API consumer?

  • @joaquinel
    @joaquinel 3 роки тому

    POINTER!!! Thanks a lot, Dr Comments.
    id is random

  • @ganjargingintahyudin9774
    @ganjargingintahyudin9774 3 роки тому

    how to operate with varchar ID ?

  • @parkuuu
    @parkuuu 2 роки тому

    Hi, I am new to databases but shouldn't the WHERE clause be id > 1009993 instead of (

    • @ggwp-gz1so
      @ggwp-gz1so Рік тому

      Depends on ur order by . Hes using desc

    • @parkuuu
      @parkuuu Рік тому +1

      @@ggwp-gz1so I was quite stupid when I wrote the comment, I just realized it now lmao thanks anyways

    • @ggwp-gz1so
      @ggwp-gz1so Рік тому

      @@parkuuu no worries happend to me xd

  • @zedmagdy
    @zedmagdy 3 роки тому

    I dont think that will work if you are using uuid instead of normal integers

  • @xiaohanghu
    @xiaohanghu Рік тому

    Only support next page, could be another solution.

  • @vigneshkaladharan75
    @vigneshkaladharan75 3 роки тому

    Great Great

  • @hussainimamman5865
    @hussainimamman5865 3 роки тому

    This will be application if you use UUID primary key

  • @vamsishankar2825
    @vamsishankar2825 3 роки тому +1

    I guess this is use case dependent solution but a nice hack, like this fails if a user wants directly to goto a page. How about make a video on that too, when user directly points to a page.

  • @AD-fs6hr
    @AD-fs6hr 2 роки тому

    isnt this same as cursor pagination?

  • @VideoMaker1
    @VideoMaker1 3 роки тому

    What you gonna do if ID will have UUID type? :)

    • @AnantKumarS
      @AnantKumarS 2 роки тому +1

      Answer by Hussein from another similar comment:
      As long as you do an order by on the UUID field the paging should work. However the problem unlike integer primary key is UUID is random so it won’t give you the “latest” entries (news in my example) when you do an order by UUID.

  • @ramesh_panthangi
    @ramesh_panthangi 3 роки тому +1

    I think its not a good idea, if we sort based on title column, ids will shuffle so it is difficult to get next page data by passing page last ID(you may get unexpected results)

  • @abdelhadisabani
    @abdelhadisabani 3 роки тому

    i use keyset pagination