<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Konstantin Kulishenkov on Medium]]></title>
        <description><![CDATA[Stories by Konstantin Kulishenkov on Medium]]></description>
        <link>https://medium.com/@konstantin.kulishenkov?source=rss-8eeaf7b05b21------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*EccXG8ECTWibso0PcXN0Yg.jpeg</url>
            <title>Stories by Konstantin Kulishenkov on Medium</title>
            <link>https://medium.com/@konstantin.kulishenkov?source=rss-8eeaf7b05b21------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 12:16:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@konstantin.kulishenkov/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Debugging composite indexes in MySQL with EXPLAIN]]></title>
            <link>https://medium.com/the-iconic-engineering/debugging-composite-indexes-in-mysql-with-explain-5c2c912cee98?source=rss-8eeaf7b05b21------2</link>
            <guid isPermaLink="false">https://medium.com/p/5c2c912cee98</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[relational-databases]]></category>
            <dc:creator><![CDATA[Konstantin Kulishenkov]]></dc:creator>
            <pubDate>Tue, 13 Aug 2019 02:27:54 GMT</pubDate>
            <atom:updated>2019-08-13T02:27:54.519Z</atom:updated>
            <content:encoded><![CDATA[<h4>How to use the key_len field in MySQL EXPLAIN output to make the best use of the composite indexes</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R4VXwtloO62JmX1qQAHlMg.jpeg" /></figure><p>The composite index in MySQL is an index on multiple columns. This kind of indexes may increase the performance of a query if it tests multiple columns in its <strong>WHERE</strong> or <strong>ORDER BY</strong> sections. Let’s see an example of such a query:</p><pre>SELECT * FROM product WHERE price = 100 AND size = &#39;Medium&#39;</pre><p>As it reads in the query, in the <strong>WHERE</strong> clause we test two different columns — <strong>price</strong> and <strong>size</strong>. Normally MySQL can use only one index per single query, so if we create two separate indexes for price and size, it won’t help — only one of them can be used in an efficient way. For example, if we had the <strong>price</strong> and <strong>size</strong> indexes, EXPLAIN output for the query above would look like this (only part of the output is shown):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/388/1*eB3vywyXPSc_C3Fa3biO7g.png" /></figure><p>Or, even worse, MySQL is very likely to decide to perform the <strong>index_merge</strong> operation on these two indexes, which usually affects performance badly (it’s a very costly operation itself).</p><p>So it seems to be the right time to add a composite index to those two columns. It’s done in a very simple way:</p><pre>CREATE INDEX price_size ON products (price, size)</pre><p>All good! Now it’s time to check if our index is applied correctly. Let’s run <strong>EXPLAIN</strong> with the query from the previous example:</p><pre>EXPLAIN SELECT * FROM product WHERE price = 100 AND size = &#39;Medium&#39;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*m2T8EzA3oOmu55v-lUOSNg.png" /></figure><p>Here we can see that our index has been used successfully. Let’s pay special attention to the <strong>key_len </strong>field. It shows the index length in bytes used in the query. The price part of the index is 6 bytes (on a <strong>DECIMAL(10, 2) </strong>column), and the size is 153 bytes (on a <strong>VARCHAR(50) </strong>column), so it makes a sum of 159 we can see in the output.</p><p>Well, a condition like <strong>price = 100</strong> doesn’t seem to be useful in real life. We’d rather filter our table with something like <strong>price &gt; 100</strong>. Let’s EXPLAIN this new query:</p><pre>EXPLAIN SELECT * FROM product WHERE price &gt; 100 AND size = &#39;Medium&#39;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*OipL770VuIGLeMy_qGAb7w.png" /></figure><p>Have you noticed a change? The <strong>key_len</strong> is just 6 now. But it was 159! It means that the index hasn’t been used fully. It’s because of the way how composite indexes are stored internally — we can roughly compare it to having the values from <strong>price</strong> and <strong>size</strong> concatenated to the same string. So for such query, MySQL can use the first part of the index (<strong>price</strong>), but can’t use the second part.</p><p>To solve this, we need to add an index with the same columns, but in a different order:</p><pre>CREATE INDEX size_price ON products (size, price)</pre><p>Is makes more sense, because we do the <strong>=</strong> comparison with the <strong>size </strong>column, so when the index starts with its value, MySQL then is also able to use the other part of this index to get the records where <strong>price &gt; 100</strong>:</p><pre>EXPLAIN SELECT * FROM product WHERE price &gt; 100 AND size = &#39;Medium&#39;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/544/1*zWUtNuv4d6DJmdxS9jn9bA.png" /></figure><p>So, we conclude that the <strong>key_len </strong>field in MySQL EXPLAIN output helps us to check if a composite index has been used fully. Also, we see that if our queries use both <strong>=</strong> and <strong>&lt;</strong> (or any other non-equal comparison), it would be better to create a composite index beginning with the column we test with <strong>=</strong> in the queries.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5c2c912cee98" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-iconic-engineering/debugging-composite-indexes-in-mysql-with-explain-5c2c912cee98">Debugging composite indexes in MySQL with EXPLAIN</a> was originally published in <a href="https://medium.com/the-iconic-engineering">THE ICONIC Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mocking and Spying DynamoDB in JS Integration Tests]]></title>
            <link>https://medium.com/the-iconic-engineering/mocking-and-spying-dynamodb-in-js-integration-tests-3b883c7efef9?source=rss-8eeaf7b05b21------2</link>
            <guid isPermaLink="false">https://medium.com/p/3b883c7efef9</guid>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[integration-testing]]></category>
            <category><![CDATA[dynamodb]]></category>
            <category><![CDATA[sinon]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Konstantin Kulishenkov]]></dc:creator>
            <pubDate>Wed, 03 Jul 2019 04:59:24 GMT</pubDate>
            <atom:updated>2019-07-03T04:59:24.949Z</atom:updated>
            <content:encoded><![CDATA[<h4>How to write a meaningful integration test for a simple Node.js backend micro-service wrapping DynamoDB</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8a98UlYf36lX3_PR8yRFkQ.jpeg" /></figure><p><strong>Integration Test </strong>is a test that a system of multiple modules (e.g. micro-service) works as expected. In this tutorial, we’ll write a “narrow” integration test, which implies that test doubles (mocks) can be used for some external dependencies (<a href="https://martinfowler.com/bliki/IntegrationTest.html">Martin Fowler’s article</a>).</p><p><strong>DynamoDB </strong>is a fast and flexible NoSQL key-value store. In our example we only need to perform two very basic operations — <strong>PutItem </strong>(create or replace) and <strong>GetItem </strong>(find by primary key).</p><p>Our micro-service implements a RESTful API. It’s written in TypeScript using the <a href="https://expressjs.com/">Express framework</a>. For DynamoDB operations we use the <a href="https://www.npmjs.com/package/aws-sdk">aws-sdk</a> package.</p><p>Let’s focus on our <strong>get </strong>operation. We need to process a simple HTTP request, at some point call the Repository class to retrieve the data from DynamoDB, and eventually return a response with something based on it.</p><p>As it was already mentioned, we’re going to write a “narrow” integration test, which means that we can use a test double for the database. In the application code, we work with the DynamoDB Client only in our Repository class. Let’s take a look at its <strong>get</strong> method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4aa54ab424ec379bd048a24a810dd1bc/href">https://medium.com/media/4aa54ab424ec379bd048a24a810dd1bc/href</a></iframe><p><strong>DocumentClient</strong> is coming with the aws-sdk npm package, where it’s already covered with tests. It’s not a part of our application code, so it seems to be a good candidate to create a test double for.</p><p>To test if our application is integrated with <strong>DocumentClient </strong>properly, we must do the following:</p><ul><li>assert that the <strong>params</strong> object we’re passing to the <strong>get</strong> method is what it’s expected to be;</li><li>enforce the <strong>get</strong> method to return the data we specified in the test</li></ul><p>To accomplish that, we’re going to use a testing lib called <a href="https://sinonjs.org/">Sinon</a>. It benefits all the JavaScript dynamic power — with Sinon we can conveniently mock/spy any function of any prototype! Let’s see how we deal with <strong>DocumentClient </strong>in our test.</p><p>First, we need to initialise the Sinon <a href="https://sinonjs.org/releases/v7.3.2/sandbox/">sandbox</a>, and since we’re going to change the object prototypes, we need to instruct Jest to restore everything in it after each test:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb44eb262bafbe286925c71f500e7b14/href">https://medium.com/media/bb44eb262bafbe286925c71f500e7b14/href</a></iframe><p>Now it’s time to initialise the <a href="https://sinonjs.org/releases/latest/stubs/">stub</a> — it can both mock the function return value and spy the params passed to it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/509081697accb937d750451bc86264c7/href">https://medium.com/media/509081697accb937d750451bc86264c7/href</a></iframe><p>Then using <a href="https://github.com/visionmedia/supertest">supertest</a> we’re all good to perform a request to our app and make the test assertions. Even though the <strong>DocumentClient</strong> instance doesn’t even exist at the moment, its prototype has been already stubbed. It means that all <strong>DocumentClient</strong> instances will have our stub behaviour until we restore the Sinon sandbox.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/62232ffc805faee3f274ffff8c2f97e6/href">https://medium.com/media/62232ffc805faee3f274ffff8c2f97e6/href</a></iframe><p>As a result, we can test our simple app end-to-end, except the third-party DynamoDB <strong>DocumentClient </strong>that we stubbed.</p><p>This article is focused more on approach than on the tool we used. So if you want to learn more about how to apply Sinon in your tests, refer to its documentation.</p><p><strong>Bonus. </strong>With this approach, we can enforce even standard built-in objects to have custom behaviour. For example, we can make <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString">Date.toString()</a> return some fixed string in our tests. Of course, it may overcomplicate things and introduce test bugs, so it doesn’t seem to be a good practice.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3b883c7efef9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-iconic-engineering/mocking-and-spying-dynamodb-in-js-integration-tests-3b883c7efef9">Mocking and Spying DynamoDB in JS Integration Tests</a> was originally published in <a href="https://medium.com/the-iconic-engineering">THE ICONIC Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>