TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Journey to Optimize Cloudflare D1 Database Queries

117 pointsby rxliuliabout 1 month ago

5 comments

fastballabout 1 month ago
I evaluated D1 for a project a few months ago, and found that global performance was pretty terrible. I don&#x27;t know what exactly the issue with their architecture is, but if you look at the time-to-first-byte numbers here[1], you can see that even for the D1 demo database the numbers outside Europe are abysmal, and even within Europe having a TTFB of &gt; 200ms isn&#x27;t great.<p>This post helps understand some basic DB pitfalls for frontend developers, but I wouldn&#x27;t use D1 regardless. If you can figure out how to use D1 as a frontend dev, you can use a hosted Postgres solution and get much more power and performance.<p>[1] <a href="https:&#x2F;&#x2F;speedvitals.com&#x2F;ttfb-test?url=https:&#x2F;&#x2F;northwind.d1sql.com&#x2F;products?_data=routes&#x2F;products" rel="nofollow">https:&#x2F;&#x2F;speedvitals.com&#x2F;ttfb-test?url=https:&#x2F;&#x2F;northwind.d1sq...</a>
评论 #43607561 未加载
评论 #43607798 未加载
评论 #43608066 未加载
评论 #43607794 未加载
评论 #43610204 未加载
评论 #43614185 未加载
your_challengerabout 1 month ago
Has anyone tried analyzing Durable Object with SQL storage performance? Is it as bad as D1?
评论 #43614808 未加载
评论 #43619583 未加载
评论 #43609889 未加载
评论 #43609833 未加载
piterrroabout 1 month ago
The optimizations listed in the article are common fallbacks of all serverless databases. Unless you are super diligent with writing queries to your database, it&#x27;s going to be costly. The only real application I found so far are small projects where less than 5 tables are needed and no JOINs are required. That means projects like: page visitor counts, mailing lists, website pageview tracking are a perfect fit for serverless databases.<p>I used Mongo serverless few years ago when it was first released, I didn&#x27;t know how the pricing works so I wasn&#x27;t aware how much these full table scans will cost me even on a small collection with 100k records...<p>For example in logdy.dev[1] I&#x27;m using D1 to collect all of the things listed above and it works like a charm with Cloudflare Workers. Just last week I published a post on how to export D1 and analyze it with Meatabase[2], for the next post I think I&#x27;m going to describe the whole stack.<p>[1]<a href="https:&#x2F;&#x2F;logdy.dev&#x2F;" rel="nofollow">https:&#x2F;&#x2F;logdy.dev&#x2F;</a> [2]<a href="https:&#x2F;&#x2F;logdy.dev&#x2F;blog&#x2F;post&#x2F;how-to-connect-cloudflare-d1-sqlite-database-with-metabase-for-powerful-analytics" rel="nofollow">https:&#x2F;&#x2F;logdy.dev&#x2F;blog&#x2F;post&#x2F;how-to-connect-cloudflare-d1-sql...</a>
评论 #43622099 未加载
评论 #43611892 未加载
kpozinabout 1 month ago
Another fun limitation is that a transaction cannot span multiple D1 requests, so you can&#x27;t select from the database, execute application logic, and then write to the database in an atomic way. At most, you can combine multiple statements into a single batch request that is executed atomically.<p>When I needed to ensure atomicity in such a multi-part &quot;transaction&quot;, I ended up making a batch request, where the first statement in the batch checks a precondition and forces a JSON parsing error if the precondition is not met, aborting the rest of the batch statements.<p><pre><code> SELECT IIF(&lt;precondition&gt;, 1, json_extract(&quot;inconsistent&quot;, &quot;$&quot;)) AS consistent FROM ... </code></pre> I was lucky here. For anything more complex, one would probably need to create tables to store temporary values, and translate a lot of application logic into SQL statements to achieve atomicity.
taf2about 1 month ago
Interesting how does the performance compare to KV or Durable Objects?
评论 #43613551 未加载