{"id":3638,"date":"2012-12-07T11:51:05","date_gmt":"2012-12-07T08:51:05","guid":{"rendered":"http:\/\/railsware.com\/blog\/?p=3638"},"modified":"2021-08-16T15:34:38","modified_gmt":"2021-08-16T12:34:38","slug":"speeding-up-authlogic-login-for-postgres-and-mysql","status":"publish","type":"post","link":"https:\/\/railsware.com\/blog\/speeding-up-authlogic-login-for-postgres-and-mysql\/","title":{"rendered":"Speeding up Authlogic login for Postgres and MySQL"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Update<\/h2>\n\n\n\n<p><em>(2012, 10 December)<\/em> Authologic-team <a href=\"https:\/\/github.com\/binarylogic\/authlogic\/commit\/d8c61db3d6179de46019fdfb074379e2711a3b96\" target=\"_blank\" rel=\"noreferrer noopener\">patched weird behavior with (I)LIKE<\/a> and now Postgres users can make everything without clutches. Just use new version 3.2.0.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>User authentication &#8211; is the most utilized part of <a href=\"https:\/\/github.com\/binarylogic\/authlogic\" target=\"_blank\" rel=\"noreferrer noopener\">Authlogic gem<\/a>. But default configuration of current version(3.1.2) doesn&#8217;t use database indexes or their usage is poor.<\/p>\n\n\n\n<p>Even so, this can be easily fixed for MySQL and with some efforts for Postgres.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Case-insensitive Login<\/h2>\n\n\n\n<p>Main reason for slow authentication &#8211; is that by default Authlogic uses case-insensitive login. For versions &lt; 3.0.1 it fetches user record with next query:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT  \"users\".* FROM \"users\"  WHERE (LOWER(\"users\".email) = 'username@gmail.com') LIMIT 1;\n<\/pre>\n\n\n\n<p>In this case Postgres and MySQL don&#8217;t use index for <em>email<\/em> field and do sequence scanning on the whole <em>users<\/em> table.<\/p>\n\n\n\n<p>To address it in version 3.0.1 Authlogic switched to &#8220;faster query&#8221;, but in fact this made things even worse. Consider next query for Postgres(for MySQL it&#8217;s plain <em>LIKE<\/em>):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT  \"users\".* FROM \"users\"  WHERE (\"users\".email ILIKE 'username@gmail.com') LIMIT 1;\n<\/pre>\n\n\n\n<p>MySQL benefits from <em>LIKE<\/em> and optimize search, but for Postgres <a href=\"http:\/\/archives.postgresql.org\/pgsql-novice\/2010-06\/msg00136.php\" target=\"_blank\" rel=\"noreferrer noopener\"><em>ILIKE<\/em> doesn&#8217;t use indexes<\/a>, so it&#8217;s just meaningless change for it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Disaster bug with (I)LIKE<\/h2>\n\n\n\n<p>With optimization this change introduced subtle bug. If user account contains &#8216;_&#8217; character or even worse &#8216;%&#8217;, <em>LIKE<\/em> use it, because Authlogic doesn&#8217;t escape these symbols.<\/p>\n\n\n\n<p>It leads to weird behavior. If you have accounts like: <em>user1@gmail.com<\/em>, and <em>user_@gmail.com<\/em>, second user will not be able to login if <em>user1<\/em> is stored before <em>user_<\/em>, because database matches <em>user1<\/em> in firsthand.<\/p>\n\n\n\n<p>But everything can be optimized without usage of <em>LIKE<\/em> for both databases.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Correct settings for MySQL<\/h2>\n\n\n\n<p>MySQL users are lucky. Know you or not, but usualy MySQL compares strings in case-insensitive manner. Just check collation for database:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql&gt; SELECT collation(version());\n+----------------------+\n| collation(version()) |\n+----------------------+\n| utf8_general_ci      |\n+----------------------+\n<\/pre>\n\n\n\n<p>If collation ends with <em>_ci<\/em>-suffix, it&#8217;s case-insensitive and you don&#8217;t need any <em>LOWER()<\/em> or <em>LIKE<\/em> clutches. Plain User.find does a trick. To activate it, disable case-insensitive mode:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class User &lt; ActiveRecord::Base\n  # ...\n  acts_as_authentic do |c|\n    c.validates_uniqueness_of_email_field_options case_sensitive: true\n    c.validates_uniqueness_of_login_field_options case_sensitive: true\n    # ... other settings\n  end\n  # ...\nend\n<\/pre>\n\n\n\n<p>Try to login in <a href=\"https:\/\/railsware.com\/web\/\" target=\"_blank\" rel=\"noreferrer noopener\">development<\/a> mode and check how Authlogic fetches user-record. It should be something like:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT \"users\".* FROM \"users\" WHERE \"users\".\"email\" = 'username@gmail.com' LIMIT 1\n<\/pre>\n\n\n\n<p>Even if your database collation isn&#8217;t <em>_ci<\/em>, you can <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.5\/en\/charset-column.html\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">easily setup custom collation for any column<\/a>.<\/p>\n\n\n\n<p>This is most simplest and plain solution for MySQL, so stick to it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configuration for Postgres<\/h2>\n\n\n\n<p><s>Sadly with current version of Authlogic it&#8217;s just impossible to optimize login for Postgres. To make it happens, <em>ILIKE<\/em> should be reverted back to <em>LOWER<\/em>-based select.<\/s> For using <em>LOWER<\/em>-based search, update Authlogic to version &gt;= 3.2.0.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">gem 'authlogic', '3.2.0'\n<\/pre>\n\n\n\n<p>Now, after switching to <em>LOWER<\/em> it&#8217;s possible to use Postgres ability to <a href=\"http:\/\/www.postgresql.org\/docs\/9.1\/static\/indexes-expressional.html\" target=\"_blank\" rel=\"noreferrer noopener\">build indexes on expressions<\/a>.<br>This migration creates an index on <em>LOWER(email)<\/em> expression:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class CreateIndexOnLowerEmailInUsersTable &lt; ActiveRecord::Migration\n  def up\n    execute &lt;&lt;-SQL\n      CREATE INDEX index_users_lower_email ON users (LOWER(email))\n    SQL\n  end\n\n  def down\n    execute &lt;&lt;-SQL\n      DROP INDEX index_users_lower_email\n    SQL\n  end\nend\n<\/pre>\n\n\n\n<p>Checking out how it performs with <em>EXPLAIN<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">EXPLAIN SELECT  \"users\".* FROM \"users\"  WHERE (LOWER(\"users\".email) = 'nickname@gmail.com') LIMIT 1;\n                                          QUERY PLAN                                          \n----------------------------------------------------------------------------------------------\n Limit  (cost=0.00..8.48 rows=1 width=1903)\n   -&gt;  Index Scan using index_users_lower_email on users  (cost=0.00..8.48 rows=1 width=1903)\n         Index Cond: (lower((email)::text) = 'nickname@gmail.com'::text)\n<\/pre>\n\n\n\n<p>Now query uses index on <em>LOWER(email)<\/em> expression. In our production environment difference between non-indexed(ILIKE) and indexed(LOWER+index) solutions is about 10x times faster for indexed-version.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Authlogic uses flawed default configuration for login-part<\/li><li>MySQL users can configure optimal behavior without a fuss<\/li><li>Postgres users should <s>patch Authlogic-gem<\/s> use authlogic-gem &gt;= 3.2.0 to apply expression indexes and gain better performance<\/li><li><s>Current Authlogic seach by <em>(I)LIKE<\/em> introduced bug and should be reverted to <em>LOWER<\/em>-based. You can support this change in comments to appropriate pull-request.<\/s> Pull-request is applied and can be used in version 3.2.0.<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Update (2012, 10 December) Authologic-team patched weird behavior with (I)LIKE and now Postgres users can make everything without clutches. Just use new version 3.2.0. Introduction User authentication &#8211; is the most utilized part of Authlogic gem. But default configuration of current version(3.1.2) doesn&#8217;t use database indexes or their usage is poor. Even so, this can&#8230;<\/p>\n","protected":false},"author":25,"featured_media":3677,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3],"tags":[],"coauthors":["Sergii Boiko"],"class_list":["post-3638","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"aioseo_notices":[],"categories_data":[{"name":"Engineering","link":"https:\/\/railsware.com\/blog?category=development"}],"post_thumbnails":"https:\/\/railsware.com\/blog\/wp-content\/themes\/railsware\/vendors\/images\/article-thumbnail-default.jpg","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/3638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/comments?post=3638"}],"version-history":[{"count":49,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/3638\/revisions"}],"predecessor-version":[{"id":14140,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/posts\/3638\/revisions\/14140"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media\/3677"}],"wp:attachment":[{"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/media?parent=3638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/categories?post=3638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/tags?post=3638"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/railsware.com\/blog\/wp-json\/wp\/v2\/coauthors?post=3638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}