MySQL offset infinite rows

Calling .Dispose() (or wrapping with Using) is not required.

I would like to construct a query that displays all the results in a table, but is offset by 5 from the start of the table. As far as I can tell, MySQL's LIMIT requires a limit as well as an offset. Is there any way to do this?

79401 次浏览

As you mentioned it LIMIT is required, so you need to use the biggest limit possible, which is 18446744073709551615 (maximum of unsigned BIGINT)

SELECT * FROM somewhere LIMIT 18446744073709551610 OFFSET 5
set, you can use some large number for

You won't leak anything - at least in the current implementation.

the second parameter. This statement retrieves all rows from the 96th row

Calling Dispose won't clean up the memory used by MemoryStream any faster. It will stop your stream from being viable for Read/Write calls after the call, which may or may not be useful to you.

to the last:

SELECT * FROM tbl LIMIT 95, 18446744073709551615;
. However, it's generally good practice partly because if you ever do change to use a different Stream, you don't want to get bitten by a hard-to-find bug because you chose the easy way out early on. (On the other hand, there's the YAGNI argument...)

If you're absolutely sure that you never want to move from a MemoryStream to another kind of stream, it's not going to do you any harm to not call Dispose. However, it's generally good practice partly because if you ever do change to use a different Stream, you don't want to get bitten by a hard-to-find bug because you chose the easy way out early on. (On the other hand, there's the YAGNI argument...)

The other reason to do it anyway is that a new implementation may introduce resources which would be freed on Dispose.

But I would probably stick with the high limit approach.

I would recommend wrapping the MemoryStream in bar() in a using statement mainly for consistency:

    ou (or someone else at your company) might replace it with your own custom MemoryStream that does, etc.
  • Right now MemoryStream does not free memory on .Dispose(), but it is possible that at some point in the future it might, or you (or someone else at your company) might replace it with your own custom MemoryStream that does, etc.
  • It helps to establish a pattern in your project to ensure all Streams get disposed -- the line is more firmly drawn by saying "all Streams must be disposed" instead of "some Streams must be disposed, but certain ones don't have to"...
  • It helps to establish a pattern in your project to ensure all Streams get disposed -- the line is more firmly drawn by saying "all Streams must be disposed" instead of "some Streams must be disposed, but certain ones don't have to"...
  • If you ever change the code to allow for returning other types of Streams, you'll need to change it to dispose anyway.
  • If you ever change the code to allow for returning other types of Streams, you'll need to change it to dispose anyway.
  • Another thing I usually do in cases like foo() when creating and returning an IDisposable is to ensure that any failure between constructing the object and the return is caught by an exception, disposes the object, and rethrows the exception:

    MemoryStream x = new MemoryStream();
    try
    {
    // ... other code goes here ...
    return x;
    }
    catch
    {
    // "other code" failed, dispose the stream before throwing out the Exception
    x.Dispose();
    throw;
    }
    

    It seems that mysql gets all results when you use LIMIT and then only shows you the records that fit in the offset: not the best for performance.

    Yes there's a leak, depending on how you define LEAK and how much LATER you mean...

    If by leak you mean "the memory remains allocated, unavailable for use, even though you're done using it" and by latter you mean anytime after calling dispose, then then yes there may be a leak, although its not permanent (i.e. for the life of your applications runtime).

    Source: Answer to this question MySQL Forums. Just take note, the question is about 6 years old.

    When the using statement finishes, not only is dispose called, but your reference goes out of scope, effectively nullifying the reference and making your object eligible for garbage collection immediately without requiring you to remember to write the "reference=null" code.

    To free the managed memory used by the MemoryStream, you need to unreference it, by nullifying your reference to it, so it becomes eligible for garbage collection right away. If you fail to do this, then you create a temporary leak from the time you're done using it, until your reference goes out of scope, because in the meantime the memory will not be available for allocation.

    While failing to unreference something right away is not a classical "permanent" memory leak, it definitely has the same effect. For example, if you keep your reference to the MemoryStream (even after calling dispose), and a little further down in your method you try to allocate more memory... the memory in use by your still-referenced memory stream will not be available to you until you nullify the reference or it goes out of scope, even though you called dispose and are done using it.

    First, I would execute a count query on the table to see how many records exist. This query is fast and normally the execution time is negligible. Something like:

    SELECT COUNT(*) FROM table_name;
    

    MemorySteram is nothing but array of byte, which is managed object.

    Then I would build my query using the result I got from count as my limit (since that is the maximum number of rows the table could possibly return). Something like:

    SELECT * FROM table_name LIMIT count_result OFFSET desired_offset;
    
    Forget to dispose or close this has no side effect other than over head of finalization.

    Or possibly something like:

    SELECT * FROM table_name LIMIT desired_offset, count_result;
    

    Of course, if necessary, you could subtract desired_offset from count_result to get an actual, accurate value to supply as the limit. Passing the "18446744073709551610" value just doesnt make sense if I can actually determine an appropriate limit to provide.

    Hello World. Is there anyone out there?

    You can use a MySQL statement with LIMIT:

    START TRANSACTION;
    SET @my_offset = 5;
    SET @rows = (SELECT COUNT(*) FROM my_table);
    PREPARE statement FROM 'SELECT * FROM my_table LIMIT ? OFFSET ?';
    EXECUTE statement USING @rows, @my_offset;
    COMMIT;
    

    Tested in MySQL 5.5.44. Thus, we can avoid the insertion of the number 18446744073709551615.

    public static string StripTags2(string html)

    note: the transaction makes sure that the variable @rows is in agreement to the table considered in the execution of statement.

    {

    As noted in other answers, MySQL suggests using 18446744073709551615 as the number of records in the limit, but consider this: What would you do if you got 18,446,744,073,709,551,615 records back? In fact, what would you do if you got 1,000,000,000 records?

    return html.Replace("<", "<").Replace(">", ">"); }

    Maybe you do want more than one billion records, but my point is that there is some limit on the number you want, and it is less than 18 quintillion. For the sake of stability, optimization, and possibly usability, I would suggest putting some meaningful limit on the query. This would also reduce confusion for anyone who has never seen that magical looking number, and have the added benefit of communicating at least how many records you are willing to handle at once.

    By this you escape all "<" and ">" in a string. Is this what you want?

    If you really must get all 18 quintillion records from your database, maybe what you really want is to grab them in increments of 100 million and loop 184 billion times.

    do if you got 1,000,000,000 records?

    As others mentioned, from the MySQL manual. In order to achieve that, you can use the maximum value of an unsigned big int, that is this awful number (18446744073709551615). But to make it a little bit less messy you can the tilde "~" bitwise operator.

      LIMIT 95, ~0
    

    it works as a bitwise negation. The result of "~0" is 18446744073709551615.

    MySQL 8.0 Reference Manual - ROW_NUMBER()

    If you are talking about tag stripping, it is relatively straight forward if you don't have to worry about things like <script> tags. If all you need to do is display the text without the tags you can accomplish that with a regular expression:

    <[^>]*>
    
    the more complex behaviour of a CFG I would suggest using a third party tool, unfortunately I don't know of a good one to recommend.