Drilling more efficiently and with less non-productive time (NPT) is one of the key enablers to reduce field development costs. In this work, we investigate the application of a data-driven optimization method called extremum seeking (ES) to achieve more efficient and safe drilling through automatic real-time minimization of the mechanical specific energy (MSE). The ES algorithm gathers information about the current downhole conditions by performing small tests with the applied weight on bit (WOB) and drill string rotational rate (RPM) while drilling and automatically implements optimization actions based on the test results. The ES method does not require an a priori model of the drilling process and can thus be applied even in instances when sufficiently accurate drilling models are not available. The proposed algorithm can handle various drilling constraints related to drilling dysfunctions and hardware limitations. The algorithm’s performance is demonstrated by simulations, where the algorithm successfully finds and maintains the optimal WOB and RPM while adhering to drilling constraints in various settings. The simulations show that the ES method is able to track changes in the optimal WOB and RPM corresponding to changes in the drilled formation. As demonstrated in the simulation scenarios, the overall improvements in rate of penetration (ROP) can be up to 20–170%, depending on the initial guess of the optimal WOB and RPM obtained from e.g., a drill-off test or a potentially inaccurate model. The presented algorithm is supplied with specific design choices and tuning considerations that facilitate its simple and efficient use in drilling applications.