Mongoid (MongoDB) has_many/belongs_to relation and wrong index being picked

Sometimes when you have a belongs_to relations, you may notice, that getting an owner object can be really slow. Even if you add index like this:

Mongoid might not catch it. Unfortunately you can’t just explain it, since this relation is returning an object not a Mongoid::Criteria. Luckily you can just hook up to your slow-log and grep for it (or you can just benchmark it). Either way, if you notice that it’s not using index as it should, first declare it the way it should be declared:

If you already had an index on a person_id, you don’t need to recreate it. It should use it out of the box. If it doesn’t help, you can always use explain to overwrite the default finder method for this relation:

 

How to increase brain’s performance

  • 1). Read More: Reading is the fastest way to learn new information. It builds focus and helps you link concepts together. It’s critical to constantly read if you want to become smarter.
  • 2). Make More Mistakes (yes, you read that right): I know this is counter-intuitive, but if you want to become smarter, how have to make more mistakes. Mistakes are the FASTEST way the brain learns. In fact, your brain hones in on the mistake and issues an “alert” to cause you to be more focused and deliberate so that you don’t make it again. Here’s a perfect example; if you’ve ever accidentally walked into a pile of dog crap on the ground, your brain makes SURE that you never do that again.
  • 3). Learn From OTHER People’s Mistakes (this is smarter than making your own mistakes): This is an even BETTER way to get smarter. Don’t make mistakes… learn from others. That’s why reading is so important (history, biographies, etc.)
  • 4). Learn to Control Your Breathing (POWERFUL ancient technique): Your breath delivers oxygen to your blood, which in turn nourishes your brain. If you want a healthy, nourished, relaxed brain, then learn to breathe deeply, calmly, and purposefully. It will make you smarter.
  • 5). Lift Weights and Sprint (sharpens your body AND brain): Exercise, even walking, delivers more oxygen to your brain. It also produces a flood of hormones in your body that lower stress, burn fat, and increase muscle growth. Bottom line, exercise boosts memory, learning ability, focus, concentration, and comprehension.
  • 6). Visualize Your Ideal Outcome (imagine being smarter): The smartest people in the world visualize what they want before it comes into existence. They visualize what they need to do to make it happen. They visualize the steps they took. They visualize the obstacles they faced. So you need to learn to visualize your ideal outcome.
  • 7). Pay Attention (focus on the little things): Intelligence is the ability to link information quickly. The easiest way to do that is to pay attention to your surroundings. Learn how to listen to what is actually being said. Learn to see the reality in front of you. Learn to be objective so that you can make quick decisions.
  • 8). Learn How to Sell: Some of the smartest, quick-witted people in the world are salespeople. If you’ve ever encountered a street salesperson, you see how intelligent they are. They can read your body language, assess your intent to buy, counter objections, and deliver your product so fast you won’t know what happened. You need to learn how to do that.
  • 9). Learn How to Program: Computer programming is phenomenal because it forces you to think logically and methodically. You learn how to deconstruct a problem and devise a solution. It’s great that kids are learning how to program in school now.
  • 10). Write Books, Articles, and Quora Answers:  It’s one thing to have knowledge in your head, but the ability to clearly and concisely write it requires a strong understanding of concepts and an ability to tailor it to your audience.
  • 11). Learn a New Language: I learned Arabic in Cairo when I was 18 and it was one of the best experiences in my entire life. Learning a new language opens up an entire world and helps you think quicker. The more mistakes you make when speaking, the faster you learn (see #2 above)
  • 12). Take More Naps: Salvador Dali and Albert Einstein were artistic and mathematical geniuses. They both took micro naps and credited their achievements to taking naps. Need I say more? Take more naps.
  • 13). Eat Less: Eating too much has been scientifically proven to lead to mild cognitive impairment. In general, the human body is resilient and adaptive, that means it does better with less. If you want to get smarter, you DON’T want to inundate your body with unnecessary calories that it has to figure out what to do with.
  • 14). Positive Self-Talk: Telling yourself “I’m smart. I’ve got this. I’ve got this under control. I love challenges. I love solving problems” is what high-achievers and world-class performers do. They don’t allow stress and worry to paralyze them. Rather, they have an extreme sense of self-reliance and inner calm that allows them to make quick, accurate decisions. That’s why I referenced the story of Jeffrey Immelt above.
  • 15). Take Aggressive, Persistent Action: Nothing makes you smarter than actually using the knowledge you have and taking ACTION. Action is what separates the dreamers from the doers. Action is what makes knowledge and intelligence worthwhile.