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.

How I Reduced My DB Server Load by 80%

201 pointsby itsderek23over 7 years ago

14 comments

brasetvikover 7 years ago
Note that Postgres supports functional indexes, and the case in the post (a `lower(column)=`-clause not being able to utilise the column&#x27;s index) is the example used in the documentation: <a href="https:&#x2F;&#x2F;www.postgresql.org&#x2F;docs&#x2F;current&#x2F;static&#x2F;indexes-expressional.html" rel="nofollow">https:&#x2F;&#x2F;www.postgresql.org&#x2F;docs&#x2F;current&#x2F;static&#x2F;indexes-expre...</a>
评论 #15436230 未加载
combatentropyover 7 years ago
This story supports my growing theory that you should put as much of your app&#x27;s rules in the database as you can.<p>There were three problems with having the rule in Rails:<p>1. The need for an index was easily overlooked.<p>2. The rule would be bypassed if a different app used the same database.<p>3. The rule wasn&#x27;t even foolproof. Only a database constraint would guarantee uniqueness when two users are saving at the same instant.<p>The problem is, SQL is hard. We should not forget how much a programmer must learn. For example: Ruby, Rails, Linux command line, HTML, CSS, JavaScript, vi, how to exit vi, etc. Each of these takes years to master.<p>SQL is especially SQuirreLy. However, it can&#x27;t possibly be worse than learning the myriad JavaScript frameworks and complicated server-build tools that are completely optional for 99% of us. My advice: Don&#x27;t do a SPA. Spend your time on SQL instead :D
评论 #15438159 未加载
评论 #15439304 未加载
评论 #15438972 未加载
评论 #15441254 未加载
评论 #15439091 未加载
评论 #15440818 未加载
cletusover 7 years ago
I&#x27;ve seen a litany of these kinds of posts and I&#x27;m always amazed by two things when I see them posted on HN:<p>1. A cadre of diehards can&#x27;t wait to post how amazing Postgress is or would be for whatever it is the OP is doing (as an aside, why isn&#x27;t Postgres more popular if it&#x27;s so amazing?); and<p>2. How averse people are to actual SQL.<p>Years ago I dealt with this crap in the Java world back when Hibernate and the like were all the rage. I was always amazed at how much confirmation bias there seemed to be. People decided these ORMs were amazing and then completely ignored all the bugs introduced by this layer and effort spent trying to figure out what the ORM was doing and how to make it do the right thing.<p>Back in the day I always liked a Java data mapper framework called iBatis (now dead, replaced by Mybatis it seems), which was pretty simple. Write some SQL in an XML file and call that SQL from your Java code. It was parameterized (so no SQL injection issues) and you could still do some funky things with discriminated types and the like. Plus, analytics were super easy because you knew how often each query was called and how long it took. Also, you could easily EXPLAIN PLAN those queries if you even had to (usually needed indexes were obvious).<p>Compare this to the auto-generated SQL from the likes of Hibernate. ugh.<p>I&#x27;ve come to the conclusion that people have this tendency to decide X is bad and then go completely out of their way to avoid X. You see it with SQL and ORMs. It largely explains (IMHO) thing slike Javascript and GWT.<p>At least half the time &quot;X is bad&quot; really means &quot;I don&#x27;t understand X and I don&#x27;t want to learn it&quot;.<p>Joel Spolsky&#x27;s &quot;leaky abstractions&quot; is good and time-honoured advice.<p>Take the Hibernate example. Once you bought into that framework you had to do all your data access that way or you broke the caching. That&#x27;s mostly bad.<p>People also overestimate their needs. They rush to create Hadoop clusters and distributed NoSQL solutions because, you know, relational DBs can&#x27;t keep up with their &quot;Big Data&quot; (which means, millions of rows) when in fact you can dump billions of rows into a single MySQL instance.
评论 #15442025 未加载
评论 #15441274 未加载
评论 #15441330 未加载
评论 #15441375 未加载
评论 #15447621 未加载
评论 #15441227 未加载
xfourover 7 years ago
Exactly why ORMs are a bad idea. I&#x27;ve always wondered whether ORMs help or harm. I feel like the one reason to use it is if you have a development team that isn&#x27;t capable of writing SQL which in itself is bad.
评论 #15435458 未加载
评论 #15435420 未加载
评论 #15435472 未加载
评论 #15435855 未加载
评论 #15435375 未加载
评论 #15436262 未加载
评论 #15436133 未加载
评论 #15436002 未加载
评论 #15435566 未加载
评论 #15435419 未加载
评论 #15435833 未加载
评论 #15435326 未加载
评论 #15440879 未加载
评论 #15438189 未加载
sidllsover 7 years ago
&gt; Rarely do we consider how one query or a series of queries could interact to slow down the whole site.<p>That doesn&#x27;t seem right to me. It&#x27;s almost always something to consider when designing the data model. Maybe I&#x27;m being uncharitable, but this seems to me to be equivalent to claiming we rarely consider the use of an algorithm or interactions between algorithms and data structures when writing some code. I mean, for toys that&#x27;s fine, but I wouldn&#x27;t defer this discussion for something I intended for public use.
petergeogheganover 7 years ago
&gt; It turns out it was coming from this line in my model. &gt; This innocuous little line was responsible for 80% of &gt; my total database load. This validates call is Rails &gt; attempting to ensure that no two Repo records get &gt; created with the same username and name. Instead of &gt; enforcing the consistency in the database, it put a &gt; before commit hook onto the object and it’s querying &gt; the database before we create a new repo to make sure &gt; there aren’t any duplicates.<p>I still can&#x27;t believe that Rails even attempts this. It&#x27;s <i>simply not possible</i> to do this kind of enforcement in a race-free manner using SELECT statements with Postgres.
DigitalJackover 7 years ago
I really like all these debug and analysis reports coming into HN lately.
评论 #15436178 未加载
ozimover 7 years ago
For all those ORM&#x27;s are bad people:<p>`I’ve been seeing that stray 30s+ spike in request time daily for months, maybe years. I never bothered to dig in because I thought it would be too much trouble to track down. It also only happened once a day, so the impact to users was pretty minimal.`
lo_fyeover 7 years ago
TL;DR - spend time configuring it, write efficient code, and understand what your 3rd party code is actually doing.
tyingqover 7 years ago
Tldr: Your ORM might generate heavyweight queries.<p>Edit: in this example, case insensitive uniqueness validation in Activerecord
throw2016over 7 years ago
Rails is a pretty popular framework and widely used. How come something like this was not caught earlier, presumably its affects all Rails applications.<p>Why would anyone code this kind of inefficiency instead of using inbuilt constraints. Has the code been reviewed, tested? Too many questions.<p>It&#x27;s surprising given how popular Rails was and still is that something which should have been caught in the early days of Rails is discovered now years later. Aren&#x27;t all the production apps seeing this? Didn&#x27;t Twitter see this?<p>The real concern is a lot of highly promoted technologies in HN do not get the proper technical scrutiny that one should take for granted in a technical forum and increasingly hype is conflated to quality.
Entangledover 7 years ago
Those who ignore CODD will learn it the hard way, forcefully.
评论 #15435999 未加载
alkzover 7 years ago
TL;DR guy has a scheduled job which puts load the db, removes a query which normally runs for 1.9ms instead of implementing rate limiting
tkyjonathanover 7 years ago
I do this everyday for the past 10 years.. This is my bread and butter. <a href="http:&#x2F;&#x2F;www.jonathanlevin.co.uk" rel="nofollow">http:&#x2F;&#x2F;www.jonathanlevin.co.uk</a>